TOJ 比赛C题 Visiting Cows (TJU 2011 Exercise Contest 04)
2011-01-30 16:48
691 查看
C. Visiting Cows
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 61 Accepted Runs: 21 Multiple test files
FJ wants Bessie to come back to the farm soon; thus, he has instructed Bessie that if two cows are directly connected by a road, she may not visit them both. Of course, Bessie would like her vacation to be as long as possible, so she would like to determine the maximum number of cows she can visit.
Lines 2..N: Each line describes a single road with two space-separated integers: C1 and C2
我是利用树状DP过的,不过Kash给我说,用topsort也可以过。。
我的代码:
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
int dp[50005][2];
vector<int>v[50005];
bool flag[50005];
int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
void dfs(int p)
{
int i;
for(i=0;i<v[p].size();i++)
{
if(!flag[v[p][i]])
{
flag[v[p][i]]=true;
dfs(v[p][i]);
}
}
if(v[p].size()==0)
{
dp[p][0]=0;
dp[p][1]=1;
}
else
{
for(i=0;i<v[p].size();i++)
{
int j=v[p][i];
dp[p][1]=dp[p][1]+min(dp[j][1],dp[j][0]);
dp[p][0]=dp[p][0]+dp[j][1];
}
dp[p][1]++;
}
}
int main()
{
int a,b,n,i;
while(scanf("%d",&n)!=EOF)
{
memset(flag,0,sizeof(flag));
for(i=0;i<50005;i++)
v[i].clear();
memset(dp,0,sizeof(dp));
memset(flag,0,sizeof(flag));
for(i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
flag[1]=true;
dfs(1);
printf("%d/n",n-min(dp[1][0],dp[1][1]));
}
return 0;
}
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 61 Accepted Runs: 21 Multiple test files
Description
After many weeks of hard work, Bessie is finally getting a vacation! Being the most social cow in the herd, she wishes to visit her N (1 ≤ N ≤ 50,000) cow friends conveniently numbered 1..N. The cows have set up quite an unusual road network with exactly N-1 roads connecting pairs of cows C1 and C2 (1 ≤ C1 ≤ N; 1 ≤ C2 ≤ N; C1 ≠ C2) in such a way that there exists a unique path of roads between any two cows.FJ wants Bessie to come back to the farm soon; thus, he has instructed Bessie that if two cows are directly connected by a road, she may not visit them both. Of course, Bessie would like her vacation to be as long as possible, so she would like to determine the maximum number of cows she can visit.
Input
Line 1: A single integer: NLines 2..N: Each line describes a single road with two space-separated integers: C1 and C2
Output
Line 1: A single integer representing the maximum number of cows that Bessie can visit.Input
Output
4
我是利用树状DP过的,不过Kash给我说,用topsort也可以过。。
我的代码:
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
int dp[50005][2];
vector<int>v[50005];
bool flag[50005];
int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
void dfs(int p)
{
int i;
for(i=0;i<v[p].size();i++)
{
if(!flag[v[p][i]])
{
flag[v[p][i]]=true;
dfs(v[p][i]);
}
}
if(v[p].size()==0)
{
dp[p][0]=0;
dp[p][1]=1;
}
else
{
for(i=0;i<v[p].size();i++)
{
int j=v[p][i];
dp[p][1]=dp[p][1]+min(dp[j][1],dp[j][0]);
dp[p][0]=dp[p][0]+dp[j][1];
}
dp[p][1]++;
}
}
int main()
{
int a,b,n,i;
while(scanf("%d",&n)!=EOF)
{
memset(flag,0,sizeof(flag));
for(i=0;i<50005;i++)
v[i].clear();
memset(dp,0,sizeof(dp));
memset(flag,0,sizeof(flag));
for(i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
flag[1]=true;
dfs(1);
printf("%d/n",n-min(dp[1][0],dp[1][1]));
}
return 0;
}
相关文章推荐
- 2011 09 04 第一周 周总结
- 记在CUDA2011编程比赛之后——基于CUDA的最短路径并行算法
- Red Hat Enterprise Linux 5.1 Server(正式版)各版本下载2011-04-17 22:21
- 04-04排列问题_奇怪的比赛
- HDU3996 Gold Mine最大权闭合图 2011 Multi-University Training Contest 16 - Host by TJU
- P1835 素数密度_NOI导刊2011提高(04)
- Imagine Cup 2011比赛中国区报名已经全面启动啦!
- [NOI2011]智能车比赛(计算几何+动态规划)
- 【分享】KDD Cup 2011(KDD Cup 2011比赛数据)
- HDU3998 Sequence 动态规划+最大流2011 Multi-University Training Contest 16 - Host by TJU
- TOJ 3651 确定比赛名次
- [比赛][ZJOI2011 day1]
- noi 2011 智能车比赛
- 2011大连regional比赛总结
- Brew----常见问题 ( 最近一次更新 2011--04--30)
- 【LOJ】#2443. 「NOI2011」智能车比赛
- 单人纸牌_NOI导刊2011提高(04)
- 一些PHP Coding Tips(php小技巧)[2011/04/02最后更新]
- P1835 素数密度_NOI导刊2011提高(04)
- 【分享】2011~2013.5全球所有足球比赛比分数据以及足彩各公司盘口