不想吐槽题目....

/*
K bulabula 算法(好像用哪个T bulabula更简单 然而我并不会 - -)
丑陋的处理cnt:
Printf时 cnt中 ans[i][0]==1 的删掉
然后更丑陋的处理强连通分量的出度 :
先把所包含的所有点f 成1 然后以每个点为起点跑一边 看看是不是出度指向本连通分量内的点
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#define maxn 1010
using namespace std;
stack<int>s;
int out[maxn];
int ans[maxn][maxn],n,m,f[maxn],cnt,vis[maxn][maxn];
int head1[maxn],head2[maxn],num;
struct node
{
int u,v,per;
}e1[maxn],e2[maxn];
void Input()
{
cin>>n>>m;
int i,a,b;
for(i=;i<=m;i++)
{
cin>>a>>b;
num++;
e1[num].u=a;
e1[num].v=b;
e1[num].per=head1[a];
head1[a]=num;
e2[num].u=b;
e2[num].v=a;
e2[num].per=head2[b];
head2[b]=num;
}
}
void Dfs1(int x)
{
int i;
f[x]=;
for(i=head1[x];i;i=e1[i].per)
if(f[e1[i].v]==)
Dfs1(e1[i].v);
s.push(x);
}
void Dfs2(int x)
{
ans[cnt][]++;
ans[cnt][ans[cnt][]]=x;
f[x]=;
int i;
for(i=head2[x];i;i=e2[i].per)
if(f[e2[i].v]==)
Dfs2(e2[i].v);
}
void Kosa()
{
int i;
for(i=;i<=n;i++)
if(f[i]==)
Dfs1(i);
memset(f,,sizeof(f));
while(!s.empty())
{
int tmp=s.top();
s.pop();
if(f[tmp]==)
{
cnt++;
Dfs2(tmp);
}
}
}
void Printf()//丑陋的处理....
{
int i,j,jian=,tot=,tmp[maxn],size;
for(i=;i<=cnt;i++)
if(ans[i][]==)jian++;//只有一个节点的 --
cout<<cnt-jian<<endl;
for(i=;i<=cnt;i++)
{
int flag=;
if(ans[i][]==)continue;//只有一个节点的 pass
memset(f,,sizeof(f));
for(j=;j<=ans[i][];j++)f[ans[i][j]]=;
for(j=;j<=ans[i][];j++)
{
int k;
for(k=head1[ans[i][j]];k;k=e1[k].per)
if(f[e1[k].v]==)
flag=;
}
if(!flag)
{
tot++;
tmp[maxn]={},size=;
for(j=;j<=ans[i][];j++)//记下答案 并排序
tmp[++size]=ans[i][j];
sort(tmp+,tmp++size);
}
}
if(tot!=)cout<<-;
else
{
for(j=;j<=size;j++)
cout<<tmp[j]<<" ";
}
}
int main()
{
Input();
Kosa();
Printf();
}

codevs 2822爱在心中的更多相关文章

  1. codevs 2822 爱在心中

    codevs 2822 爱在心中  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description “每个人都拥有一个梦,即使彼此不相同, ...

  2. codevs——2822 爱在心中

    2822 爱在心中  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description “每个人都拥有一个梦,即使彼此不相 ...

  3. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  4. 【wikioi】2822 爱在心中

    题目链接 算法:Tarjan+dfs(最短路的都行,判连通而已) 先了解一下什么是Tarjan Tarjan算法用于求出图中所有的强连通分量. 转自NOCOW:点击打开链接 ============= ...

  5. 【CodeVS】2822 爱在心中 [2017年6月计划 强连通分量03]

    2822 爱在心中 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够 ...

  6. codevs2822 爱在心中

      2822 爱在心中 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无 ...

  7. 【codevs2822】爱在心中 tarjan 缩点+理解

    [codevs2822]爱在心中 2014年1月26日5580 题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸, ...

  8. 【CodeVS 2822】爱在心中

    “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度里有N个人,在他们的心中都有着一个 ...

  9. 爱在心中(codevs 2822)

    题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度 ...

随机推荐

  1. bzoj 1501: [NOI2005]智慧珠游戏 Dancing Link

    1501: [NOI2005]智慧珠游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 122[Submit][Status] ...

  2. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  3. hdu 2012 素数判定 Miller_Rabbin

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. E: Write error - write (28 No space left on device)

    1:在终端中运行cd命令,提示: e: Write error - write (28 No space left on device) E: Cant mmap an empty file 2:使用 ...

  5. 【MyBatis学习笔记】

    [MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...

  6. Windows Azure 配置SSTP

    方法參考下面文章的步驟. 这个成功.http://freevpnba.com/windows-azure-sstp-vpn/ 这个没成功,不知道为什么.http://diaosbook.com/pos ...

  7. Park Visit

    hdu4607:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题意:给你一棵树,树上每条边的权值是1,然后然你选择m个点,求遍历m个点的最小花费. 题解 ...

  8. git入门超详细(转载)

    转自:http://www.cnblogs.com/tugenhua0707/p/4050072.html Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SV ...

  9. Nova 无法向虚机注入密钥

    欢迎各位关注我的博客:http://weibo.com/u/216633637 废话开头: 之前参考这位同学的博客http://www.cnblogs.com/awy-blog/p/3447176.h ...

  10. android进程间通信:使用AIDL

    android 的binder其实是基于 openbinder实现的,openbinder的地址:http://www.angryredplanet.com/~hackbod/openbinder/d ...