传送门

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解决 核心代码
 int dfs(int now)
{
int i;
if(dp[now]>) return dp[now];
dp[now]=;
for(i=;i<G[now].size();i++){
dp[now]=max(dp[now],dfs(G[now][i])+);
}
return dp[now];
}
10702991                 2015-04-14 14:15:05     njczy2010     A - Reposts             GNU C++     Accepted 15 ms 40 KB
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string> #define ll long long
int const N = ;
int const M = ;
int const INF = 0x3f3f3f3f;
ll const mod = ; using namespace std; int n;
char s1[N],s2[N],te[N];
map<string,int> mp;
int tot;
int ans;
vector<int> G[N];
int dp[N]; void ini()
{
memset(dp,,sizeof(dp));
ans=tot=;
int i,j,l1,l2;
mp.clear();
for(i=;i<=;i++){
G[i].clear();
}
for(i=;i<=n;i++){
scanf("%s%s%s",s2,te,s1);
l1=strlen(s1);
l2=strlen(s2);
for(j=;j<l1;j++){
if(s1[j]>='A' && s1[j]<='Z'){
s1[j]=s1[j]-'A'+'a';
}
}
for(j=;j<l2;j++){
if(s2[j]>='A' && s2[j]<='Z'){
s2[j]=s2[j]-'A'+'a';
}
}
if(mp.count(s1)==){
tot++;
mp[s1]=tot;
}
if(mp.count(s2)==){
tot++;
mp[s2]=tot;
}
G[ mp[s1] ].push_back( mp[s2] );
}
} int dfs(int now)
{
int i;
if(dp[now]>) return dp[now];
dp[now]=;
for(i=;i<G[now].size();i++){
dp[now]=max(dp[now],dfs(G[now][i])+);
}
return dp[now];
} void solve()
{
ans=dfs();
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
}

VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]的更多相关文章

  1. VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树

    题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...

  2. codeforces VK Cup 2015 - Qualification Round 1 B. Photo to Remember 水题

    B. Photo to Remember Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/522/ ...

  3. Codeforces VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线线段树 求区间相同数的最小距离

    D. Closest Equals Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/prob ...

  4. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  5. VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟

    C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  6. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  7. VK Cup 2012 Qualification Round 1---C. Cd and pwd commands

    Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland

    今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...

  9. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

随机推荐

  1. android动画之android:interpolator属性使用

    android动画之android:interpolator使用 Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerat ...

  2. Js学习文件上传

    // 文件上传 jQuery(function() { var $ = jQuery, $list = $('#thelist'), $btn = $('#ctlBtn'), state = 'pen ...

  3. 利用Jenkins打包ISO和QCOW2镜像文件

    现在的云虚拟化环境越来越多,经常会碰到需要修改并重新打包新的ISO或QCOW2镜像文件.通过手工的方式会比较麻烦,所以在镜像发布的生产环境中可以利用Jenkins来进行定期打包发布,以下介绍Jenki ...

  4. Two-Phase Commit (2PC)

    两阶段提交模式像极了比赛发令:“预备,开始!”

  5. js Math 对象

    Math 对象方法 方法 描述 abs(x) 返回数的绝对值. acos(x) 返回数的反余弦值. asin(x) 返回数的反正弦值. atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值 ...

  6. 1.ssm web项目中的遇到的坑--自定义JQuery插件(slide menu)

    自定义的JQuery插件写的回调函数不执行: 写好了回调函数,将函数打印出来是原形,就是不执行 function () { console.log("---onClickItem---&qu ...

  7. Angular缺少 FormsModule

    虽然 ngModel是一个有效的 Angular 指令,不过它在默认情况下是不可用的. 解决方法: 在根模块引入FormModule import { FormsModule } from '@ang ...

  8. nginx解决跨域(前后端分离)

    Nginx解决跨域问题 后端接口 请求地址 返回数据(json数据) http://127.0.0.1:8080//app Hello World! 前端代码 通过nginx做静态资源服务器访问端口8 ...

  9. SV学习之interface

    普通的模块使用法:注意我们这里只实现了部分功能....不是完全的读写模块....     module mem_core(   input logic wen,  input logic ren,   ...

  10. hadoop上传文件报错

    19/06/06 16:09:26 INFO hdfs.DFSClient: Exception in createBlockOutputStream java.io.IOException: Bad ...