传送门

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. poj2441 Arrange the Bulls

    思路: 状态压缩dp.需要一点优化,否则容易超时. 实现: #include <cstdio> #include <vector> #include <cstring&g ...

  2. (3)《Head First HTML与CSS》学习笔记---CSS入门

    1.O‘Reilly的<CSS PocketReference>是一本不错的CSS参考小书,记录了常用的元素属性. 2.元素选择器的作用强于继承的作用:用户定义强于浏览器默认(以下所有讨论 ...

  3. 安卓自定义View教程目录

    基础篇 安卓自定义View基础 - 坐标系 安卓自定义View基础 - 角度弧度 安卓自定义View基础 - 颜色 进阶篇 安卓自定义View进阶 - 分类和流程 安卓自定义View进阶 - Canv ...

  4. http与WebSocket

    利用websocket连接服务器的最大特点就是:持久链接的特点. 共同点是:都是基于TCP协议进行client-server的链接,websocket是HTML5提出的一套补缺HTTP链接中不能持久链 ...

  5. 《Python基础教程》 读书笔记 第六章 抽象 函数 参数

    6.1创建函数 函数是可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> x=1 ...

  6. Java之流水号生成器实现

    参考:https://www.jianshu.com/p/331b872e9c8f 1.建立一张存放的表 CREATE TABLE `sys_serial_number` ( `id` bigint( ...

  7. linux之awk命令

    一.awk的内置参数 $0:表示整个当前行 $1:每行第一个字段 $2:每行第二个字段 $n:每行第n个字段 awk的参数:分隔符 -F separator 设定分隔符(默认为空格) 打印单个字段: ...

  8. JQQ文字素材

    1.十二生肖:子鼠.丑牛.寅虎.卯兔.辰龙.巳舍.午马.未羊.申猴.酉鸡.戌狗.亥猪.丙申年(2016)乙未年(2015)甲午年(2014)癸巳年(2013)壬辰年(2012)辛卯年(2011)庚寅年 ...

  9. Linux目录结构及详细介绍

    /:根目录,位于Linux文件系统目录结构的顶层,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中. /bin,/usr/bin:该 ...

  10. python 人脸识别试水(一)

    1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip  pip 版本10 4.安装 numpy    :pip ins ...