标签:
比起数组优点有三:
1.代码量少
2.fch再也不用想着清-1,不用纠结反向弧
3.更好理解了
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(‘ ‘) 8 #define ENT putchar(‘\n‘) 9 using namespace std; 10 const int maxn=100000+10,maxm=100000+10,inf=-1u>>1; 11 struct BFS{ 12 struct ted{int x,y;ted*next;}adj[maxm],*fch[maxn],*ms; 13 bool vis[maxn];int n; 14 void init(int n){ 15 this->n=n;ms=adj;memset(vis,false,sizeof(vis));return; 16 } 17 void ade(int u,int v){*ms=(ted){u,v,fch[u]};fch[u]=ms++;return;} 18 void bfs(int x){ 19 queue<int>Q;Q.push(x);vis[x]=true; 20 while(!Q.empty()){ 21 int u=Q.front();Q.pop(); 22 for(ted*e=fch[u];e;e=e->next){ 23 int v=e->y;if(!vis[v]) vis[v]=true,Q.push(v); 24 } 25 } return; 26 } 27 }p1,p2; 28 inline int read(){ 29 int x=0,sig=1;char ch=getchar(); 30 while(!isdigit(ch)){if(ch==‘-‘)sig=-1;ch=getchar();} 31 while(isdigit(ch))x=10*x+ch-‘0‘,ch=getchar(); 32 return x*=sig; 33 } 34 inline void write(int x){ 35 if(x==0){putchar(‘0‘);return;}if(x<0)putchar(‘-‘),x=-x; 36 int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; 37 for(int i=len-1;i>=0;i--)putchar(buf[i]+‘0‘);return; 38 } 39 int n,m; 40 void init(){ 41 n=read();m=read(); 42 p1.init(n);p2.init(n); 43 int x,y; 44 for(int i=1;i<=m;i++){ 45 x=read();y=read(); 46 p1.ade(x,y); 47 p2.ade(y,x); 48 } p1.bfs(1);p2.bfs(1); 49 for(int i=1;i<=n;i++){ 50 if(p1.vis[i]&&p2.vis[i]) write(i),ENT; 51 } 52 return; 53 } 54 void work(){ 55 return; 56 } 57 void print(){ 58 return; 59 } 60 int main(){init();work();print();return 0;}
标签:
原文地址:http://www.cnblogs.com/chxer/p/4593229.html