Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)
Input
The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.
Output
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes 问:
给出的图是否存在对任意的u 和 v 要么u -> v, 要么 v -> u,这两者是或者的关系 不是并且
先求强连通,因为每个强连通分量中的点都可以相互到达,然后缩点 求最小路径覆盖
网上都是用的拓扑求最长路径 但最小路径覆盖就是这个思想
所以最后只要判断是否只有一个最小路径即可
代码就是改了一下HDU 3861的输出 所以就是水题啦
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff; int n, m, s, t; vector<int> G[maxn];
int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> S; void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); i ++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v])
{
low[u] = min(low[u], pre[v]);
}
}
if(low[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} int cur[maxn], head[maxn], cnt, d[maxn], nex[maxn << ]; struct node{
int u, v, c;
}Node[maxn << ]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
d[s] = ;
Q.push(s);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i!= -; i = nex[i])
{
int v = Node[i].v;
if(!d[v] && Node[i].c > )
{
d[v] = d[u] + ;
Q.push(v);
if(v == t) break;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u];i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] == d[u] + && Node[i].c > )
{
int V = dfs(v, min(Node[i].c, cap));
Node[i].c -= V;
Node[i ^ ].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
return ret;
} int Dinic()
{
int ret = ;
while(bfs())
{
memcpy(cur, head, sizeof head);
ret += dfs(s, INF);
}
return ret;
} int graph[][]; int main()
{
int T;
rd(T);
while(T--)
{
int u, v;
mem(head, -);
cnt = ;
rd(n), rd(m);
mem(sccno, );
mem(pre, );
dfs_clock = scc_cnt = ;
for(int i = ; i <= n; i++) G[i].clear();
for(int i = ; i <= m; i++)
{
int u, v;
rd(u), rd(v);
G[u].push_back(v);
}
for(int i = ; i <= n; i ++) if(!pre[i]) dfs(i);
s = , t = maxn - ;
rap(u, , n)
{
for(int i = ; i < G[u].size(); i ++)
{
int v = G[u][i];
//cout << sccno[u] << " " << sccno[v] << endl;
if(sccno[u] != sccno[v])
add(sccno[u], scc_cnt + sccno[v], );
}
}
rap(i, , scc_cnt)
add(s, i, ), add(scc_cnt + i, t, );
if(scc_cnt - Dinic() == )
printf("Yes\n");
else
printf("No\n"); } return ;
}
Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)的更多相关文章
- Oracle基本数据字典:v$database、v$instance、v$version、dba_objects
v$database: 视图结构: SQL> desc v$database; Name Null? Type - ...
- POJ2762 Going from u to v or from v to u(单连通 缩点)
判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage
SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...
- [强连通分量] POJ 2762 Going from u to v or from v to u?
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17089 ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- poj 2762 Going from u to v or from v to u?
题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...
- POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
随机推荐
- c# jobject 的数据结构的解析:
c# jobject 数据结构的解析:首先下载Newtonsoft.Json,增加引用using Newtonsoft.Json.Linq;把jobject的内容提取出来,Jobject的内容格式如下 ...
- dubbo 2.7.0 中缺乏 <dubbo:annotation /> 的解决方案
一.背景 从 dubbo 2.6.5 升级到 2.7.0,突然发现好多地方不能用了,dubbo:annotation 直接报红,原先的 @Service 和 @Reference 中直接报了过时,源 ...
- 亿级流量场景下,大型架构设计实现【2】---storm篇
承接之前的博:亿级流量场景下,大型缓存架构设计实现 续写本博客: ****************** start: 接下来,我们是要讲解商品详情页缓存架构,缓存预热和解决方案,缓存预热可能导致整个系 ...
- 使用Android服务,实现报警管理器和广播接收器
介绍 几乎在所有平台上都有很多进程运行背景,它们被称为服务.可能在Android平台中有一些服务可以执行长时间运行的操作,这些操作在处理时不需要用户交互. 在本文中,借助预定义的Android警报服务 ...
- TCP点对点转发的实现与原理(nodejs)
Nagent Nagent是TCP点对点转发实现,名称来源于Nat与Agent的组合.类似frp项目,可以在局域网与互联网提供桥梁. 前提是你要有一台流量服务器并且有一个公网IP.如果没有,也可以找服 ...
- c#实现用SQL池(多线程),定时批量执行SQL语句 【转】
在实际项目开发中,业务逻辑层的处理速度往往很快,特别是在开发Socket通信服务的时候,网络传输很快,但是一旦加上数据库操作,性能一落千丈,数据库操作的效率往往成为一个系统整体性能的瓶颈.面对这问题, ...
- ERROR:"org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /config/topics/test" when creating or deleting Kafka operations authorized through the Ranger policies
PROBLEM DESCRIPTION When creating or deleting topics in Kafka, they cannot be authorized through the ...
- SQLserver2008一对多,多行数据显示在一行
现在有一个需求 我们有一张表employee EmpID EmpName ---------- ------------- 张山 张大山 张小山 李菲菲 李晓梅 Result I need in th ...
- LOJ #6042. 「雅礼集训 2017 Day7」跳蚤王国的宰相
我可以大喊一声这就是个思博题吗? 首先如果你能快速把握题目的意思后,就会发现题目就是让你求出每个点要成为树的重心至少要嫁接多少边 先说一个显然的结论,重心的答案为\(0\)(废话) 然后我们考虑贪心处 ...
- EntityFramework Core 2.1重新梳理系列属性映射(一)
前言 满血复活啦,大概有三个月的时间没更新博客了,关于EF Core最新进展这三个月也没怎么去看,不知现阶段有何变化没,本文将以EF Core 2.1稳定版本作为重新梳理系列,希望对看本文的你有所帮助 ...