codeforces 522 A. Reposts
http://codeforces.com/problemset/problem/522/A 题意:给定某条消息的传播路径。问最远传播的距离。。 思路:其实就是问树的深度。。直接dfs就行了。。
存的时候用map<string,vector
/* ***********************************************
Author :111qqz
Created Time :2015年12月05日 星期六 16时41分41秒
File Name :code/cf/problem/522A.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=2E2+7;
7map<string,vector<string> > mp;
8map<string,bool>vis;
9string from,to,nouse;
10int ans;
11int n;
1void dfs(string cur,int depth)
2{
3// cout<<"cur:"<<cur<<" depth:"<<depth<<endl;
4 vis[cur] = true;
5 ans = max(ans,depth);
6 for ( int i = 0 ; i < mp[cur].size() ; i++)
7 {
8 string v = mp[cur][i];
9 if (!vis[v])
10 dfs(v,depth+1);
11 }
12}
1string tran(string x)
2{
3 int len = x.length();
4 for ( int i = 0 ; i < len ; i++)
5 {
6 if (x[i]>='A'&&x[i]<='Z')
7 {
8 x[i] = char(x[i]+32);
9 }
10 }
11 return x;
12}
13int main()
14{
15 #ifndef ONLINE_JUDGE
16 freopen("code/in.txt","r",stdin);
17 #endif
18 mp.clear();
19 scanf("%d",&n);
20// getchar();
21 for ( int i = 0 ; i < n ; i++)
22 {
23 cin>>to>>nouse>>from;
24 to = tran(to);
25 from = tran(from);
26// getchar();
27 // cout<<"to:"<<to<<endl<<" nouse:"<<nouse<<endl<<" from:"<<from<<endl;
28 mp[from].push_back(to);
29 }
30 ans = 1;
31 dfs("polycarp",1);
32 printf("%d\n",ans);
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}