题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26882

思路:一开始把题意理解错了,还以为是简单路径,然后仔细一看发现是一条路径,意思就是说从起点出发,把所有的点走一遍,于是就要考虑强连通分量,因为对于同一个强连通分量的点是相互可达的,于是我们可以先缩点,建新图,统计新图中顶点的入度与出度的关系,判断即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<list>
using namespace std;
#define MAXN 1111
#define inf 1<<30
#define INF 1LL<<60
#define FILL(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int,int>PP;
template<class T> inline T Get_MIN(const T &a,const T &b){ return a < b ? a : b; }
template<class T> inline T Get_MAX(const T &a,const T &b){ return a > b ? a : b; }
template<class T> inline T ABS(const T &a){ return a < ? -a : a; } int n,m,k;
bool vis[MAXN];
vector<vector<int> >g,num;
map<int,int>ID; int low[MAXN],dfn[MAXN],color[MAXN];
int scc_count,cnt;
bool mark[MAXN];
stack<int>S; void Tarjan(int u)
{
low[u]=dfn[u]=++cnt;
mark[u]=true;
S.push(u);
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(dfn[v]==){
Tarjan(v);
low[u]=Get_MIN(low[u],low[v]);
}else if(mark[v]){
low[u]=Get_MIN(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc_count++;
int v;
do{
v=S.top();
S.pop();
mark[v]=false;
color[v]=scc_count;
}while(u!=v);
}
} int In_degree[MAXN],Out_degree[MAXN];
void Build()
{
FILL(In_degree,);
FILL(Out_degree,);
for(int u=;u<=m;u++){
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(color[u]!=color[v]){
In_degree[color[v]]++;
Out_degree[color[u]]++;
}
}
}
} int main()
{
int _case,u,v,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d",&n);
g.clear();
g.resize();
num.clear();
num.resize();
ID.clear();
FILL(vis,false);
m=;
for(int i=;i<=n;i++){
scanf("%d",&k);
while(k--){
scanf("%d%d",&u,&v);
if(!vis[u])vis[u]=true,ID[u]=++m;
if(!vis[v])vis[v]=true,ID[v]=++m;
g[ID[u]].push_back(ID[v]);
}
}
FILL(dfn,);
FILL(mark,false);
scc_count=cnt=;
for(int i=;i<=m;i++){
if(dfn[i]==)Tarjan(i);
}
printf("Case %d: ",t++);
if(scc_count==){
puts("YES");
continue;
}
Build();
if(Out_degree[color[ID[]]]==||In_degree[color[ID[]]]!=){
puts("NO");
continue;
}
int cnt1,cnt2,cnt3;
cnt1=cnt2=cnt3=;
for(int i=;i<=scc_count;i++){
if(In_degree[i]==&&Out_degree[i]==)cnt1++;
else if(In_degree[i]==&&Out_degree[i]==)cnt2++;
else if(In_degree[i]==&&Out_degree[i]==)cnt3++;
}
if(cnt1==&&cnt2==&&cnt1+cnt2+cnt3==scc_count){
puts("YES");
}else
puts("NO");
}
return ;
}

loj 1168(Tarjan应用)的更多相关文章

  1. loj 1026( tarjan + 输出割边 )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1026 思路:Tarjan 算法简单应用.割边的特点:low[v]>dfn[u]( ...

  2. 浅谈Tarjan算法

    从这里开始 预备知识 两个数组 Tarjan 算法的应用 求割点和割边 求点-双连通分量 求边-双连通分量 求强连通分量 预备知识 设无向图$G_{0} = (V_{0}, E_{0})$,其中$V_ ...

  3. LOJ 2547 「JSOI2018」防御网络——思路+环DP

    题目:https://loj.ac/problem/2547 一条树边 cr->v 会被计算 ( n-siz[v] ) * siz[v] 次.一条环边会被计算几次呢?于是去写了斯坦纳树. #in ...

  4. ⌈洛谷5058⌋⌈ZJOI2004⌋嗅探器【Tarjan】

    题目连接 [洛谷传送门] [LOJ传送门] 题目描述 某军搞信息对抗实战演习,红军成功地侵入了蓝军的内部网络,蓝军共有两个信息中心,红军计划在某台中间服务器上安装一个嗅探器,从而能够侦听到两个信息中心 ...

  5. LOJ #2587「APIO2018」铁人两项

    是不是$ vector$存图非常慢啊...... 题意:求数对$(x,y,z)$的数量使得存在一条$x$到$z$的路径上经过$y$,要求$x,y,z$两两不同  LOJ #2587 $ Solutio ...

  6. Tarjan求强连通分量 缩点

    强连通分量的定义: 在一张有向图中,如果两个点u,v之间能相互到达则称这两个点u,v是强连通的,在这个基础上如果有向图G中的任意两个顶点都强连通,那么称图G是一个强连通图.有向非强连通图的极大强连通子 ...

  7. Loj 6036 「雅礼集训 2017 Day4」编码 - 2-sat

    题目传送门 唯一的传送门 题目大意 给定$n$个串,每个串只包含 ' .问是否可能任意两个不同的串不满足一个是另一个的前缀. 2-sat的是显然的. 枚举每个通配符填0还是1,然后插入Trie树. 对 ...

  8. loj#2305. 「NOI2017」游戏 2-sat

    链接 https://loj.ac/problem/2305 https://www.luogu.org/problemnew/show/P3825 思路 3-sat神马的就不要想了,NP问题 除去x ...

  9. LOJ 2587 「APIO2018」铁人两项——圆方树

    题目:https://loj.ac/problem/2587 先写了 47 分暴力. 对于 n<=50 的部分, n3 枚举三个点,把图的圆方树建出来,合法条件是 c 是 s -> f 路 ...

随机推荐

  1. wes开发笔记

    html中的button和submit有什么不同? submit是提交表单用,而button是执行javascript用,两者各有用处. 用到自己写按钮的时候,都是用button,submit很少写 ...

  2. httpd/php/mysql的安装-1

    rely:  依赖: relier, reliance , reliable : i don't think he is a reliable man . we must lay stress on ...

  3. Javascript中理解发布--订阅模式

    Javascript中理解发布--订阅模式 阅读目录 发布订阅模式介绍 如何实现发布--订阅模式? 发布---订阅模式的代码封装 如何取消订阅事件? 全局--发布订阅对象代码封装 理解模块间通信 回到 ...

  4. 用了那么久居然没发现firefox快捷键有如此多

    firefox用了也有好几年了,除了ytkah经常用到的搜索Ctrl+F.加入书签Ctrl+D.打开新便签Ctrl+T,鼠标右键菜单,还安装了鼠标手势插件FireGestures,快速关闭标签.撤销关 ...

  5. Crashing Robots(imitate)

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8124   Accepted: 3528 D ...

  6. Summarize Series For Burying My College

    Summarize  Series  For  Burying  My  College                                             For  Grade ...

  7. 输入的全角字符转换成半角字符--css、js、ASP.NET

    我们经常需要用户在表单中输入数字,用户不小心使用了全角状态输入数字,但是在程序中全角的数字是不能直接转换为数字的.这种全角数字的错误处理起来就有些麻烦了.对一些没有经验用户来说明明已经输入数字了,怎么 ...

  8. 第19章 使用PXE+Kickstart部署无人值守安装

    章节概述: 本章节将教会您通过PXE+DHCP+TFTP+VSftpd+Kickstart服务程序搭建出无人值守安装系统,从而批量部署客户机系统. 这种系统能够实现自动化运维.避免了重复性劳动,帮助提 ...

  9. poj1789 Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  10. Linux prerouting和postrouting的区别

    我大概清楚一点就是从内网出去的时候用POSTROUTING进来的时候用PREROUTING,可是做透明代理的时候确是用PREROUTING.这是为什么呢? 回复: sunnygg pre还是post是 ...