PKU1470+LCA
简单LCA。。。。但是输入的时候很恶心。。。。
看discuss,改了scanf才过了。。。
1 /* 2 LCA 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 #include<map> 11 #include<math.h> 12 using namespace std; 13 typedef long long ll; 14 //typedef __int64 int64; 15 const int maxn = 1005; 16 const int inf = 0x7fffffff; 17 const double pi=acos(-1.0); 18 const double eps = 1e-8; 19 int dis[ maxn ]; 20 int ace[ maxn ],deep[ maxn ],fa[ maxn ]; 21 int res[ maxn ]; 22 struct node{ 23 int u,next; 24 }edge[ maxn<<4 ]; 25 int cnt,head[ maxn ]; 26 void init(){ 27 memset( res,0,sizeof( res ) ); 28 memset( fa,0,sizeof( fa ) ); 29 cnt = 0; 30 memset( head,-1,sizeof( head ) ); 31 } 32 void addedge( int a,int b ){ 33 edge[ cnt ].u = b; 34 edge[ cnt ].next = head[ a ]; 35 head[ a ] = cnt++; 36 } 37 void dfs( int now,int now_father,int now_ace,int now_deep ){ 38 fa[ now ] = now_father; 39 ace[ now ] = now_ace; 40 deep[ now ] = now_deep; 41 for( int i=head[ now ];i!=-1;i=edge[ i ].next ){ 42 int v = edge[ i ].u; 43 dfs( v,now,now_ace,now_deep+1 ); 44 } 45 } 46 int find( int x,int y ){ 47 if( x==y ) return x; 48 if( deep[x]>deep[y] ) return find( fa[x],y ); 49 else return find( x,fa[y] ); 50 } 51 int main(){ 52 int n; 53 while( scanf("%d",&n)==1 ){ 54 int cur,num,nxt; 55 init(); 56 for( int i=1;i<=n;i++ ){ 57 scanf("%d:(%d)",&cur,&num); 58 while( num-- ){ 59 scanf(" %d",&nxt); 60 addedge( cur,nxt ); 61 } 62 } 63 for( int i=1;i<=n;i++ ){ 64 if( fa[i]==0 ){ 65 dfs( i,-1,i,0 ); 66 } 67 } 68 int m; 69 scanf("%d",&m); 70 //printf("m=%d\n",m); 71 char ch1[2],ch2[2]; 72 while( m-- ){ 73 scanf("%1s%d%d%1s",ch1,&cur,&nxt,ch2); 74 int father = find( cur,nxt ); 75 res[ father ]++; 76 } 77 //printf("m=%d\n",m); 78 for( int i=1;i<=n;i++ ){ 79 if( res[i]!=0 ){ 80 printf("%d:%d\n",i,res[i]); 81 } 82 } 83 } 84 return 0; 85 }
keep moving...