`
lilisalo
  • 浏览: 1108436 次
文章分类
社区版块
存档分类
最新评论

TOJ 比赛C题 Visiting Cows (TJU 2011 Exercise Contest 04)

 
阅读更多
C. Visiting Cows
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 ≤ C1N; 1 ≤ C2N; C1C2) 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: N
  • Lines 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;

}

分享到:
评论

相关推荐

    toj程序代码

    以前在队内时在toj上写的代码。题目网址http://acm.tju.edu.cn/toj/

    TOJ习题:《数据结构》-邓俊辉-清华大学1

    TOJ习题:《数据结构》-邓俊辉-清华大学1

    CSharpt ToJ ava Converter 22.4.20 完美版

    CSharpt ToJ ava Converter 22.4.20 完美版 不再有转换行数限制。完美利器

    toj题目

    toj离线版!!!!!!!!!!!

    toj-dev:新版TOJ是一个Virtual onlinejudge,支持主流不同OJ并支持不同比赛 ,题目tag,题目推荐等功能

    托杰夫 新TOJ是支持不同OJ和竞赛的虚拟在线裁判。 此外,它还具有更多功能,例如问题... 前端是用Node.js编写的,而判断器是用C ++编写的。 现在,每个OJ都有一个判断器,但是我计划将每个OJ都配置为一个通用判断器。

    final_toj

    final_toj

    MIAC-tfcisOJ:检查TOJ页面中的问题是否被接受

    To check if the problems in the page of TOJ are Accepted. Usage Click " Download ZIP " Right click the file and do "解压缩至此" Then, open the folder. Open " execute " with text editor(右键编辑...

    保留字母1095

    台州学院的TOJ题目 1095题,运行可行。编一个程序,输入一个字符串,将组成字符串的所有非英文字母的字符删除后输出。 输入 一个字符串,长度不超过80个字符。 输出 删掉非英文字母后的字符串。 样例输入 abc123+xyz...

    1007. jseph.cpp

    toj上的一道算法题。这个你们知道的。参考一下而已

    某高校ACM内部培训资料

    ACM训练计划,这些题都来自浙大(ZJU)、北大(PKU)和同济(TOJ)的网站。 有些题的解法几乎一眼可以看出,但对C,C++基本语句的熟悉还是很有好处的。练习这些题还有以下目的: 1. 了解竞赛题的形式 2. 熟悉如何在训练...

    ACM.zip_www .acm 1024.com

    ACM练习题 zju1024 zju1940 zju2097 pku2503 toj2196 zju1091 zju1507 zju1649 graph bfs

    NOI/NOIP中的DP(动态规划)类型

    这类题是属于徘徊在DP与递归之间得一类题,本质是类似于记忆化搜索的一种填表,有很强的数学味。 7、线段覆盖问题 改版:Tom的烦恼(TOJ)等。经常利用到离散化等技巧辅助。 8、 ……………… ………………

    基于Deep Belief Nets 的中文名实体关系抽取

    自然语言处理 实体关系抽取 Deep Belief Nets

    C++实现九连环(改良版)

    做题做到TOJ 3318 Chinese Rings 故此借鉴 Mohrie 的C++递归实现九连环http://download.csdn.net/source/770975 仍可计算步数、每步操作 可循环输入 以输入0结束 运行环境:Dev-C++

    dushubiji.rar_具体数学_数学笔记

    具体数学读书笔记,转自toj wtommy博课 ACM选手数学必看佳作

    采用API函数和PictureBox自制的旋转图像控件

    2、方法:除一般运行方法外,配置了Refresh、picPaintPicture、picPoint、picPset、openPicFile (PicFile As String)、PicRotate(ToJ As Single)。尤其后两种,可设置打开的图片文件以及图片旋转角度。应用方法示例...

    leetcode中国-fuck_offer:fuck_offer

    leetcode中国 Fuck Offer ACMer & JudgeOnline POJ : 北京大学Online Judge ...SGU,TOJ,SPOJ都适合区域赛的ACMer训练冲金 USACO : USACO是美国中学生的官方竞赛网站 CodeForces : Codefores是俄罗斯

    利用Python登录学校OJ爬取AC代码

    之前用Python爬取了Toj的题目主干——简单Python爬虫练习,并以此作为Python爬虫的入门实验,待我Python能力有所长进后,就想到或许可以使用Python把自己之前提交的AC代码全部爬下来,这也许会很有趣。

    清华大学上机考试刷题指南

    本人参加过清华夏令营和九推,这是题主整理的上机考试刷题指南,包括常见的考题和答题思路。

    antonio-solucweb.github.io

    由@ An​​toj21开发的编辑器,尤其是对于Steem社区来说,是为了使这个出色平台的所有成员更轻松地编辑自己的帖子 我试图使界面尽可能简单友好,以帮助理解和流畅地编辑其内容,毕竟,这样做的目的是简化任务,而...

Global site tag (gtag.js) - Google Analytics