http://codeforces.com/contest/950/problem/E
贴一份板子
1 #include<cstdio> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 struct E 6 { 7 int to,nxt; 8 }e[200100]; 9 int f1[100100],ne,a[100100],sccc,sccnum[100100],top,dfn[100100],low[100100],dfsc; 10 int s[100100],n,m,h,ou[100100],anss=0x3f3f3f3f,ans1; 11 vector<int> sccx[100100]; 12 void me(int u,int v) 13 { 14 e[++ne].to=v; 15 e[ne].nxt=f1[u]; 16 f1[u]=ne; 17 } 18 19 void dfs(int u) 20 { 21 dfn[u]=low[u]=++dfsc; 22 s[++top]=u; 23 for(int k=f1[u],v;k!=0;k=e[k].nxt) 24 { 25 v=e[k].to; 26 if(!dfn[v]) 27 { 28 dfs(v); 29 low[u]=min(low[u],low[v]); 30 } 31 else if(!sccnum[v]) 32 low[u]=min(low[u],dfn[v]); 33 } 34 if(low[u]==dfn[u]) 35 { 36 sccc++; 37 while(top&&s[top]!=u) sccnum[s[top--]]=sccc; 38 sccnum[s[top--]]=sccc; 39 } 40 } 41 42 int main() 43 { 44 int c1,c2,i,k; 45 scanf("%d%d%d",&n,&m,&h); 46 for(i=1;i<=n;i++) scanf("%d",&a[i]); 47 for(i=1;i<=m;i++) 48 { 49 scanf("%d%d",&c1,&c2); 50 if((a[c1]+1)%h==a[c2]) me(c1,c2); 51 if((a[c2]+1)%h==a[c1]) me(c2,c1); 52 } 53 for(i=1;i<=n;i++) 54 if(!dfn[i]) 55 dfs(i); 56 for(i=1;i<=n;i++) 57 for(k=f1[i];k!=0;k=e[k].nxt) 58 if(sccnum[e[k].to]!=sccnum[i]) 59 ou[sccnum[i]]++; 60 for(i=1;i<=n;i++) 61 sccx[sccnum[i]].push_back(i); 62 //printf("%da ",ou[1]); 63 for(i=1;i<=sccc;i++) 64 if(ou[i]==0&&sccx[i].size()<anss) 65 { 66 anss=sccx[i].size(); 67 ans1=i; 68 } 69 printf("%d ",anss); 70 for(i=0;i<anss;i++) printf("%d ",sccx[ans1][i]); 71 return 0; 72 }