VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]
1 second
256 megabytes
standard input
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.
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.
Print a single integer — the maximum length of a repost chain.
5 tourist reposted Polycarp Petr reposted Tourist WJMZBMR reposted Petr sdya reposted wjmzbmr vepifanov reposted sdya
6
6 Mike reposted Polycarp Max reposted Polycarp EveryOne reposted Polycarp 111 reposted Polycarp VkCup reposted Polycarp Codeforces reposted Polycarp
2
1 SoMeStRaNgEgUe reposted PoLyCaRp
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上最长路 ]的更多相关文章
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- 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/ ...
- 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 ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- 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 ...
- 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 ...
- 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 ...
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland
今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...
- 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 ...
随机推荐
- http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
- JQuery日期选择器插件date-input
JQuery日期选择器插件之date-input 官方网站:http://jonathanleighton.com/projects/date-input/ 下载地址: http://github.c ...
- 应用-如何使不同的企业使用独自的数据源。使用ejb3.0+jboss6.2EAP+JPA
摘要: 如何使不同的企业使用独自的数据源.使用ejb3.0+jboss6.2EAP+JPA10C应用系统被多个企业同时使用,为了提供个性化服务,如何使不同的企业使用独自的 ...
- 登录脚本重构Element
登录脚本重构Element package com.gubai.selenium; import org.openqa.selenium.By; import org.openqa.selenium. ...
- Netbeans Makefile: recipe for target 'XXX' failed 运行failed(退出值 -1073741511 找不到C/C++库文件,关键字
今天不知怎么的又出错了 吐血了 找不到NULL new等关键字了 看到知乎上有人和我一个问题,怎么办?很简单卸载了以前的cygwin和netbeans然后重装,我重装时没有把以前的cygwin ...
- Python 语言规范
Python 语言规范 pychecker 对你的代码运行pychecker 定义: pychecker 是一个在Python 源代码中查找bug 的工具. 对于C 和C++这样的不那 么动态的( ...
- centos7 取消Ctrl+Alt+Del重启功能
转载:http://www.cnblogs.com/huangjc/p/4536620.html Linux默认允许任何人按下Ctrl+Alt+Del来重启系统.但是在生产环境中,应该停用按下Ctrl ...
- 事件绑定、取消的二种形式 & call
<script> //call 函数下的一个方法,call方法第一个参数可以改变函数执行过程中的内部this的指向,call方法第二个参数开始就是原来函数的参数列表. function f ...
- mount nfs 各版本之间的转换
[root@one1-fst-hx ~]# mount.nfs 182.168.2.49:/mnt/sdb/nfs /mnt/nfs2/ nomand,-o vers=3[root@one1-fst- ...
- docker的安装及基础操作与镜像构建
仓库配置及安装启动 [root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 [root@loca ...