http://www.lightoj.com/volume_showproblem.php?problem=1168

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 using namespace std;
  5 #define N 1005
  6 struct{
  7     int y,next;
  8 }ee[10*N];
  9 struct{
 10     int a,b;
 11 }mes[10*N];
 12 int link[N],dfn[N],low[N],pos[N],stack[N],t,st,bct,ind,in[N],out[N];
 13 bool vis[N],instack[N],inn[N];
 14 void init()
 15 {
 16     memset(link,0,sizeof(link));
 17     memset(inn,0,sizeof(inn));
 18     memset(out,0,sizeof(out));
 19     memset(vis,0,sizeof(vis));
 20     memset(instack,0,sizeof(instack));
 21     memset(in,0,sizeof(in));
 22     t=0,st=0,bct=0,ind=0;
 23 }
 24 void tarjan(int rt)
 25 {
 26     low[rt]=dfn[rt]=++ind;
 27     vis[rt]=instack[rt]=1;
 28     stack[++st]=rt;
 29     for(int i=link[rt];i;i=ee[i].next){
 30         if(!vis[ee[i].y]){
 31             tarjan(ee[i].y);
 32             low[rt]=min(low[rt],low[ee[i].y]);
 33         }
 34         else if(instack[ee[i].y]){
 35             low[rt]=min(low[rt],dfn[ee[i].y]);
 36         }
 37     }
 38     if(low[rt]==dfn[rt]){
 39         bct++;
 40         while(1){
 41             int c=stack[st];
 42             pos[c]=bct;
 43             instack[c]=0;
 44             st--;
 45             if(c==rt)break;
 46         }
 47     }
 48 }
 49 void insert(int a,int b)
 50 {
 51     ee[++t].y=b;
 52     ee[t].next=link[a];
 53     link[a]=t;
 54 }
 55 void rebuild(int m)
 56 {
 57     memset(link,0,sizeof(link));
 58     t=0;
 59     for(int i=1;i<=m;i++){
 60         if(pos[mes[i].a]!=pos[mes[i].b]){
 61             insert(pos[mes[i].a],pos[mes[i].b]);
 62             out[pos[mes[i].a]]++;
 63             in[pos[mes[i].b]]++;
 64         }
 65     }
 66 }
 67 int main()
 68 {
 69     int tt,cas=1;
 70     scanf("%d",&tt);
 71     while(tt--){
 72         int n,m=0,k;
 73         init();
 74         inn[0]=1;
 75         scanf("%d",&n);
 76         for(int i=1;i<=n;i++){
 77             scanf("%d",&k);
 78             while(k--){
 79                 int a,b;
 80                 scanf("%d%d",&a,&b);
 81                 insert(a,b);
 82                 inn[a]=1;
 83                 inn[b]=1;
 84                 m++;
 85                 mes[m].a=a,mes[m].b=b;
 86             }
 87         }
 88         for(int i=0;i<1000;i++)
 89         if(!vis[i]&&inn[i])
 90             tarjan(i);
 91         if(bct==1){
 92             printf("Case %d: YES\n",cas++);
 93             continue;
 94         }
 95         rebuild(m);
 96        
 97         if(in[pos[0]]!=0||out[pos[0]]==0){
 98             printf("Case %d: NO\n",cas++);
 99             continue;
100         }
101         int cnt1=0,cnt2=0,cnt3=0;
102         for(int i=1;i<=bct;i++){
103             if(in[i]==0&&out[i]==1){
104                 cnt1++;
105             }
106             else if(in[i]==1&&out[i]==0){
107                 cnt2++;
108             }
109             else if(in[i]==1&&out[i]==1)
110                 cnt3++;
111         }
112         if(cnt1==1&&cnt2==1&&cnt1+cnt2+cnt3==bct)
113         printf("Case %d: YES\n",cas++);
114         else
115         printf("Case %d: NO\n",cas++);
116     }
117     return 0;
118 }
AC Code