| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我用邻接表都TLE了,真郁闷!!#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
struct node{
int destiny;
node *next;
node(int ds,node *tmp){
destiny=ds;
next=tmp;
}
} *n[1010];
int num,qstn;
int mark[1010];
queue<int> que;
void add(int p,int q){
node *tmp=new node(q,n[p]);
n[p]=tmp;
}
void del(int p,int q){
node *cur=n[p];
node *delnode=NULL;
node *nextnode=NULL;
if(cur->destiny==q){
n[p]=n[p]->next;
return;
}
while(1){
if(cur->next->destiny==q){
delnode=cur;
nextnode=cur->next->next;
break;
}
cur=cur->next;
}
delnode->next=nextnode;
}
/*
void print(){
int p;
for(p=1;p<=num;p++){
node *cur=n[p];
cout<<p<<"->";
while(cur){
cout<<cur->destiny<<"->";
cur=cur->next;
}
cout<<endl;
}
}
*/
void bfs(){
int curn=que.front();
que.pop();
int i,j,k;
node *cnode=n[curn];
while(cnode){
int des=cnode->destiny;
if(mark[des]==0){
que.push(des);
mark[des]=1;
}
cnode=cnode->next;
}
}
void main(){
// ifstream cin("data.txt");
cin>>num>>qstn;
int i,j,k;
for(i=1;i<=qstn;i++){
char c;
cin>>c>>j>>k;
if(c=='Q'){
// cout<<j<<" "<<k<<endl;
int tmp;
for(tmp=1;tmp<=num;tmp++)mark[tmp]=0;
while(!que.empty())que.pop();
mark[j]=1;
que.push(j);
while(!que.empty()&&mark[k]==0){bfs();}
if(que.empty())cout<<"N"<<endl;
else cout<<"Y"<<endl;
}else if(c=='I'){
add(j,k);
add(k,j);
// print();
}else if(c=='D'){
// cout<<"delete"<<j<<" "<<k<<endl;
del(j,k);
del(k,j);
// print();
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator