codevs2822爱在心中
来源:互联网 发布:安徽省大数据产业协会 编辑:程序博客网 时间:2024/05/06 23:02
2822 爱在心中
时间限制: 1 s
空间限制: 128000 KB
题目描述 Description
“每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动。爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home。”
在爱的国度里有N个人,在他们的心中都有着一个爱的名单,上面记载着他所爱的人(不会出现自爱的情况)。爱是具有传递性的,即如果A爱B,B爱C,则A也爱C。
如果有这样一部分人,他们彼此都相爱,则他们就超越了一切的限制,用集体的爱化身成为一个爱心天使。
现在,我们想知道在这个爱的国度里会出现多少爱心天使。而且,如果某个爱心天使被其他所有人或爱心天使所爱则请输出这个爱心天使是由哪些人构成的,否则输出-1。
输入描述 Input Description
第1行,两个数N、M,代表爱的国度里有N个人,爱的关系有M条。
第2到第M+1行,每行两个数A、B,代表A爱B。
输出描述 Output Description
第1行,一个数,代表爱的国度里有多少爱心天使。
第2行,如果某个爱心天使被其他所有人和爱心天使所爱则请输出这个爱心天使是由哪些人构成的(从小到大排序),否则输出-1。
样例输入 Sample Input
样例输入1:
6 7
1 2
2 3
3 2
4 2
4 5
5 6
6 4
样例输入2:
3 3
1 2
2 1
2 3
样例输出 Sample Output
样例输出1:
2
2 3
样例输出2:
1
-1
数据范围及提示 Data Size & Hint
各个测试点1s
tarjan模板题。。
今晚两节自习课都在计划着go home。。
于是匆匆地和班里同学告别便走了。。
走的有些仓促。。
附上本蒟蒻的代码:
#include<cstdio>#include<iostream>using namespace std;struct data{ int to,next;}edge[50001],d[50001];int n,m,cnt,top,dfn[10001],h[10001],low[10001],q[10001],succ,head[10001],belong[10001],have[10001];bool vis[10001],mark[10001];int read(){ int w=0,c=1; char ch=getchar(); while (ch<'0' || ch>'9') { if (ch=='-') c=-1; ch=getchar(); } while (ch>='0' && ch<='9') w=w*10+ch-'0',ch=getchar(); return w*c;}void dfs(int x){ int now; vis[x]=mark[x]=true,low[x]=dfn[x]=++cnt,q[++top]=x; int c=head[x]; while (c) { if (!vis[edge[c].to]) dfs(edge[c].to),low[x]=min(low[x],low[edge[c].to]); else if (mark[edge[c].to]) low[x]=min(low[x],dfn[edge[c].to]); c=edge[c].next; } if (low[x]==dfn[x]) { succ++; while (now!=x) now=q[top--],mark[now]=0,belong[now]=succ,++have[succ]; }}void rebuild(){ cnt=0; for (int i=1;i<=n;i++) { int c=head[i]; while (c) { if (belong[i]!=belong[edge[c].to]) d[++cnt].to=belong[edge[c].to],d[cnt].next=h[belong[i]],h[belong[i]]=cnt; c=edge[c].next; } }}void tarjan(){ for (int i=1;i<=n;i++) if (!vis[i]) dfs(i); rebuild();}int main(){ int i,x,y,ans=0; n=read(),m=read(); for (i=1;i<=m;i++) { x=read(),y=read(); edge[i].to=y,edge[i].next=head[x],head[x]=i; } tarjan(); for (i=1;i<=succ;i++) if (have[i]>1) ans++; printf("%d\n",ans); ans=-1; for (i=1;i<=succ;i++) if (!h[i]) if (ans!=-1 || have[i]==1) { ans=-1; break; } else ans=i; if (ans==-1) printf("%d\n",ans); else for (i=1;i<=n;i++) if (belong[i]==ans) printf("%d ",i); return 0;}
- codevs2822 爱在心中
- Codevs2822 爱在心中
- codevs2822爱在心中
- codevs2822爱在心中
- 【codevs2822】爱在心中
- Codevs2822爱在心中题解
- [CodeVS2822]爱在心中 做题笔记
- 【基础练习】【强连通tarjan】codevs2822 爱在心中题解
- 【codevs2822】 爱在心中 tarjan+统计点的个数
- codevs2822 爱在心中(tarjan求缩点的度)
- 爱,因为在心中
- wikioi 爱在心中
- 爱在心中
- 爱在心中
- 1626 爱在心中
- 爱在心中
- 爱在心中 CODEVS
- 【wikioi】2822 爱在心中
- iOS的系统框架
- 最流行的android组件大全
- mysql常用命令大全
- php生成的csv, 无法完整显示带前导0的数字
- POJ 1094 Sorting It All Out【拓扑排序】
- codevs2822爱在心中
- FastCV Public API最新版本1.7.1概述
- Systrace Walkthrough
- SpannableString与SpannableStringBuilder DecimalFormat 和 BigFormat
- lightoj 1319 - Monkey Tradition (中国剩余定理)
- Android新手入门2016(13)--阻塞对话框PopupWindow
- Linux内核源码分析方法
- 【LeetCode】131.Palindrome Partitioning回文划分
- youtube下载视频好工具插件