VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]

本文介绍了一个计算社交网络中笑话转发链最大长度的问题,并提供了一种使用记忆化深度优先搜索(DFS)来解决此问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

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++    Accepted15 ms40 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\n",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 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4425825.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值