BZOJ 1051: [HAOI2006]受欢迎的牛 缩点
2015-11-05 21:59
393 查看
1051: [HAOI2006]受欢迎的牛
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=1051Description
每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受欢迎的。Under two situations the player could score one point.
⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.
⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.
There are three types of players.
Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it's difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.
There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.
Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.
The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?
[b]Input[/b]
第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可能出现多个A,B)
[b]Output[/b]
一个数,即有多少头牛被所有的牛认为是受欢迎的。
[b]Sample Input[/b]
3 3 1 2 2 1 2 3
[b]Sample Output[/b]
1
HINT
[b]题意[/b]
[b]题解:[/b]
缩点,然后看出度为0 的点是否只有一个,如果只有一个就输出这个点的大小
否则就是0
[b]代码[/b]
#include<iostream> #include<stdio.h> #include<vector> #include<cstring> using namespace std; #define maxn 10005 vector<int> E[maxn]; vector<int> rE[maxn]; int belong[maxn]; int vis[maxn]; int cl[maxn]; int cnt = 0; int siz[maxn]; int kis[maxn]; void dfs1(int x) { vis[x]=1; for(int i=0;i<E[x].size();i++) { int e = E[x][i]; if(vis[e])continue; dfs1(e); } cl[++cnt]=x; } void dfs2(int x) { vis[x]=1; belong[x]=cnt; siz[cnt]++; for(int i=0;i<rE[x].size();i++) { int e = rE[x][i]; if(vis[e])continue; dfs2(e); } } int main() { int n,m;scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int x,y;scanf("%d%d",&x,&y); E[x].push_back(y); rE[y].push_back(x); } for(int i=1;i<=n;i++) { if(!vis[i]) dfs1(i); } memset(vis,0,sizeof(vis)); cnt = 0; for(int i=n;i>=1;i--) { if(!vis[cl[i]]) { cnt++; dfs2(cl[i]); } } for(int i=1;i<=n;i++) { for(int j=0;j<E[i].size();j++) { if(belong[i]==belong[E[i][j]]) continue; kis[belong[i]]++; } } int ans1=0,ans2=0; for(int i=1;i<=cnt;i++) { if(kis[i]==0) { ans1++; ans2=i; } } if(ans1==1) printf("%d\n",siz[ans2]); else puts("0"); }
相关文章推荐
- 归档 NSKeyedArchiver
- Spring下ActiveMQ实战
- Reverse Nodes in k-Group 解答
- C#总结项目《影院售票系统》编写总结三
- Binary Heap
- C_C++_Java Java利用JNI调用C的函数
- linux下安装erlang
- Android事件拦截/分发/响应 机制
- LeetCode---Search a 2D Matrix
- HDOJ 3371 Connect the Cities 【Prime】
- 如何设置修改WPS批注上的用户信息名称
- 检测文件(夹)大小
- [转]浅谈dijkstra堆优化
- UIView总结
- Ceph集群中如何摘除一个包含mon、osd和mds的节点
- 【Android开发—智能家居系列】(三):手机连接WIFI模块
- Android IOS WebRTC 音视频开发总结(五十)-- 技术服务如何定价?
- UIScrollView
- 【Xcode】【iOS-pod】The sandbox is not sync with the Podfile.lock
- JCenter下载太慢?教你修改Maven仓库地址为国内镜像