传送门

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. (5)《Head First HTML与CSS》学习笔记---布局与定位

    层叠与CSS的权重判断 1.要理解层叠,除了前面的内容外还差最后一个知识点.你已经知道如何使用多个样式表来更好地组织你的样式,或者支持不同类型的设备.不过实际上用户访问你的页面时还有另外一些样式表. ...

  2. JS正则表达式匹配<div><style>标签

    测试字符串: <style>v\:* {                 BEHAVIOR: url(#default#VML) } o\:* {                 BEHA ...

  3. 洛谷 P1803 凌乱的yyy

    题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...

  4. docker 应用数据的管理之bind mounts

    创建容器使用bind mounts 挂载文件系统.宿主机文件系统会覆盖掉容器里初始数据 [root@localhost ~]# mkdir /www/htpm -pv mkdir: 已创建目录 &qu ...

  5. 用户交互和if条件判断、嵌套

    #a=input("提示语“)#接受的数据类型是字符串str#提示用户输入姓名 # a=input("请输入姓名") print(a) '''输出结果:请输入姓名小明 姓 ...

  6. linux 隐藏进程

    1.首先推荐一个后门程序https://github.com/f0rb1dd3n/Reptile 具体可以了解一下功能非常强大. 2.源码如下 root@ubuntu:/var/srt/libproc ...

  7. PHP16 PHP访问MySQL

    学习要点 PHP访问MySQL配置 PHP访问MySQL函数介绍 足球赛程信息管理 PHP访问MySQL配置 PHP.ini配置文件确认以下配置已经打开 extension=php_mysql.dll ...

  8. Java sleep方法的作用(sleep())

    sleep() 方法的作用是在指定的毫秒数内让当前“正在执行的线程”休眠(暂停执行).这个“正在执行的线程”是指 this.currentThread() 返回的线程. 例 1 下面通过一个案例来理解 ...

  9. 总结Java开发者经常会犯的前十种错误

    [导读] 在Java中,有些事物如果不了解的话,很容易就会用错,如数组转换为数组列表.元素删除.Hashtable和HashMap.ArrayList和LinkedList.Super和Sub构造函数 ...

  10. NFS和DHCP服务

    1. NFS NFS,Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS: NFS允许一个系统在网络上与他人共享目录和文件 ...