VK Cup 2015 - Qualification Round 1 A. Reposts [dp DAG에서 가장 긴 길]
A. Reposts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.
These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2"user "name1"didn't have the joke in his feed yet, and "name2"already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp"and initially the joke was only in his feed.
Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.
Input
The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.
We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.
Output
Print a single integer — the maximum length of a repost chain.
Sample test(s)
Input
5 tourist reposted Polycarp Petr reposted Tourist WJMZBMR reposted Petr sdya reposted wjmzbmr vepifanov reposted sdya
Output
6
Input
6 Mike reposted Polycarp Max reposted Polycarp EveryOne reposted Polycarp 111 reposted Polycarp VkCup reposted Polycarp Codeforces reposted Polycarp
Output
2
Input
1 SoMeStRaNgEgUe reposted PoLyCaRp
Output
2
:
DAG , dp
1 int dfs(int now)
2 {
3 int i;
4 if(dp[now]>0) return dp[now];
5 dp[now]=1;
6 for(i=0;i<G[now].size();i++){
7 dp[now]=max(dp[now],dfs(G[now][i])+1);
8 }
9 return dp[now];
10 }
10702991
2015-04-14 14:15:05
njczy2010
A - Reposts
GNU C++
Accepted
15 ms
40 KB
1 #include <cstdio>
2 #include <cstring>
3 #include <stack>
4 #include <vector>
5 #include <algorithm>
6 #include <queue>
7 #include <map>
8 #include <string>
9
10 #define ll long long
11 int const N = 405;
12 int const M = 205;
13 int const INF = 0x3f3f3f3f;
14 ll const mod = 1000000007;
15
16 using namespace std;
17
18 int n;
19 char s1[N],s2[N],te[N];
20 map<string,int> mp;
21 int tot;
22 int ans;
23 vector<int> G[N];
24 int dp[N];
25
26 void ini()
27 {
28 memset(dp,0,sizeof(dp));
29 ans=tot=0;
30 int i,j,l1,l2;
31 mp.clear();
32 for(i=0;i<=200;i++){
33 G[i].clear();
34 }
35 for(i=1;i<=n;i++){
36 scanf("%s%s%s",s2,te,s1);
37 l1=strlen(s1);
38 l2=strlen(s2);
39 for(j=0;j<l1;j++){
40 if(s1[j]>='A' && s1[j]<='Z'){
41 s1[j]=s1[j]-'A'+'a';
42 }
43 }
44 for(j=0;j<l2;j++){
45 if(s2[j]>='A' && s2[j]<='Z'){
46 s2[j]=s2[j]-'A'+'a';
47 }
48 }
49 if(mp.count(s1)==0){
50 tot++;
51 mp[s1]=tot;
52 }
53 if(mp.count(s2)==0){
54 tot++;
55 mp[s2]=tot;
56 }
57 G[ mp[s1] ].push_back( mp[s2] );
58 }
59 }
60
61 int dfs(int now)
62 {
63 int i;
64 if(dp[now]>0) return dp[now];
65 dp[now]=1;
66 for(i=0;i<G[now].size();i++){
67 dp[now]=max(dp[now],dfs(G[now][i])+1);
68 }
69 return dp[now];
70 }
71
72 void solve()
73 {
74 ans=dfs(1);
75 }
76
77 void out()
78 {
79 printf("%d
",ans);
80 }
81
82 int main()
83 {
84 //freopen("data.in","r",stdin);
85 //scanf("%d",&T);
86 //for(int cnt=1;cnt<=T;cnt++)
87 //while(T--)
88 while(scanf("%d",&n)!=EOF)
89 {
90 ini();
91 solve();
92 out();
93 }
94 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.