codevs2822
解题思路: tarjan缩点后算出度为0的点有几个,如果只有一个且这个点为爱心天使就行了;
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#define maxn 10005
using namespace std;
struct node
{
int x;
int y;
}a[maxn];
struct Edge
{
int to;
int next;
}edge[maxn];
int low[maxn];
int dfn[maxn];
int instack[maxn];
int visit[maxn];
int sccno[maxn];
int outdegree[maxn];
int cnt;
int step;
int indexx;
int scc_cnt;
int head[maxn];
vector<int>scc[maxn];
void add(int u,int v)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
head[u]=cnt++;
return;
}
void tarjan(int u)
{
low[u]=dfn[u]=++step;
instack[++indexx]=u;
visit[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(!dfn[edge[i].to])
{
tarjan(edge[i].to);
low[u]=min(low[u],low[edge[i].to]);
}
else if(visit[edge[i].to])
{
low[u]=min(low[u],dfn[edge[i].to]);
}
}
if(low[u]==dfn[u])
{
scc_cnt++;
scc[scc_cnt].clear();
do
{
scc[scc_cnt].push_back(instack[indexx]);
sccno[instack[indexx]]=scc_cnt;
visit[instack[indexx]]=0;
indexx--;
}
while(u!=instack[indexx+1]);
}
return;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=step=indexx=scc_cnt=0;
memset(visit,0,sizeof(visit));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
return ;
}
int main()
{
int n,m;
int x,y;
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
add(a[i].x,a[i].y);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
{
if(sccno[a[i].x]!=sccno[a[i].y])
outdegree[sccno[a[i].x]]++;
}
int flag=0;
int pos;
for(int i=1;i<=scc_cnt;i++)
{
if(outdegree[i]==0)
{
flag++;pos=i;
}
}
int ans=0;
for(int i=1;i<=scc_cnt;i++)
{
if(scc[i].size()>1)
ans++;
}
cout<<ans<<endl;
if(flag==1)
{
if(scc[pos].size()>1)
{
sort(scc[pos].begin(),scc[pos].end());
for(int i=0;i<scc[pos].size();i++)
cout<<scc[pos][i]<<" ";
}
else
cout<<"-1\n";
}
else
cout<<"-1\n";
return 0;
}
codevs2822的更多相关文章
- 【codevs2822】爱在心中 tarjan 缩点+理解
[codevs2822]爱在心中 2014年1月26日5580 题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸, ...
- codevs2822 爱在心中
2822 爱在心中 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无 ...
- 【codevs2822】爱在心中
题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度 ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 1051: [HAOI2006]受欢迎的牛 (tarjan强连通分量+缩点)
题目大意:CodeVs2822的简单版本 传送门 $Tarjan$强连通分量+缩点,若连通块的个数等于一则输出n:若缩点后图中出度为0的点个数为1,输出对应连通块内的点数:否则输出0: 代码中注释部分 ...
- Tarjan模板——求强连通分量
Tarjan求强连通分量的流程在这个博客讲的很清楚,再加上我也没理解透,这里就不写了. 缩点:将同一个连通块内的点视为同一个点. 扔一道模板题:codeVS2822爱在心中 第一问很显然就是求点数大于 ...
随机推荐
- [LOJ#2386]. 「USACO 2018.01 Platinum」Cow at Large[点分治]
题意 题目链接 分析 假设当前的根为 rt ,我们能够在奶牛到达 \(u\) 之时拦住它,当且仅当到叶子节点到 \(u\) 的最短距离 \(mn_u \le dis_u\) .容易发现,合法的区域是许 ...
- 《Python从菜鸟到高手》已经出版,开始连载了,购买送视频课程
好消息,<Python从菜鸟到高手>已经出版!!! JetBrains官方推荐图书!JetBrains官大中华区市场部经理赵磊作序!送2400分钟同步视频课程!500个案例,400道P ...
- 如何解决 Windows 实例出现身份验证错误及更正 CredSSP
阿里云上的ESC赠送1核2G服务器,安装windows server 2016 Datacenter 3389远程登录时提示错误信息,参考阿里文档:https://help.aliyun.com/kn ...
- Log4.Net 在Winform、MVC、ashx程序里的使用,ashx程序里使用异步
最近做一个双11活动的,是一套相关的H5页面.本来以为难度不大,但是做下来几天还是遇到些问题.就总结一下吧,还是有收获的. 1.在H5页面中,有一个遮罩层,还是挺有意思的.直接用div+css控制遮罩 ...
- heb Daz
Asatras soi bib Daz! gos la haik ri, dewoa gos mi haik quri. soi Fong d cuup va ti Chusan, imps Dabo ...
- CodeForces 719A. Vitya in the Countryside
链接:[http://codeforces.com/group/1EzrFFyOc0/contest/719/problem/A] 题意: 给你一个数列(0, 1, 2, 3, 4, 5, 6, 7, ...
- (FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
- 【学习总结】GirlsInAI ML-diary day-3-数据类型
[学习总结]GirlsInAI ML-diary 总 原博github链接-day3 数据类型 熟悉一下计算时可能碰到的数据类型.(计算时...) 1-打开jupyter,new一个新python文件 ...
- Handling duplicate form submission in Spring MVC
javaweb开发之防止表单重复提交 - u012843873的博客 - CSDN博客 https://blog.csdn.net/u012843873/article/details/5526212 ...
- Docker bridge br0 pipework
Docker Centos7 下建立 Docker 桥接网络 - weifengCorp - 博客园https://www.cnblogs.com/weifeng1463/p/7468497.html ...