当前位置:网站首页>poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
2022-07-05 23:16:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
Serie A Champion : Given a digraph has m Unidirectional edge . Free inference whether two points up (a Sure b or b Sure a Or at most each other ), The request
Weak connection weight .
Algorithm :
Shrink the point to find the strongly connected component . Then build the map again . Infer whether the new graph is a single chain , That is, you can't fork , If it forks, there will be inaccessible situations .
How to infer whether it is a single chain ?
It means that each entry is 0 There is only one point , That is, there is only one point in the queue each time .
( o(╯□╰)o....
. It seems to have been used for the second time pair Record the point pairs of the original drawing , Then save pair Of vector Forgetting to empty leads to wa Come on wa Go to !
)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#define maxn 1010
#define maxm 20010
using namespace std;
struct node
{
int to,next;
}edge[maxm],edge2[maxm];
int head[maxn],cnt,n;
int clk,top,s[maxn],scc,dfn[maxn],low[maxn],belong[maxn];
bool instack[maxn],vis[maxn];
int head2[maxn],cnt2,in[maxn];
typedef pair<int,int> PII;
vector<PII> xx;
queue<int> q;
void add(int x,int y)
{
edge[cnt].to = y;
edge[cnt].next = head[x];
head[x] = cnt++;
}
void add2(int x,int y)
{
edge2[cnt2].to = y;
edge2[cnt2].next = head2[x];
head2[x] = cnt2++;
}
void dfs(int x)
{
dfn[x] = low[x] = clk++;
s[top++] = x;
instack[x] = true;
for(int i=head[x];i!=-1;i = edge[i].next)
{
int u = edge[i].to;
if(dfn[u]==-1)
{
dfs(u);
low[x] = min(low[u],low[x]);
}
else if(instack[u])
{
low[x] = min(low[x],dfn[u]);
}
}
if(low[x]==dfn[x])
{
int u;
scc++;
do
{
u = s[--top];
instack[u]=false;
belong[u] = scc;
}while(u!=x);
}
}
void tarjan()
{
memset(dfn,-1,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
clk = top = scc = 0;
for(int i=1;i<=n;i++)
{
if(dfn[i]==-1)
dfs(i);
}
}
bool topo()
{
memset(vis,0,sizeof(vis));
int c = 0;
while(!q.empty())
q.pop();
for(int i=1;i<=scc;i++)
{
if(!in[i])
{
c++;
q.push(i);
}
}
if(c>1) return false;
while(!q.empty())
{
int u = q.front();
q.pop();
if(vis[u]) continue;
vis[u] = true;
c = 0;
for(int i=head2[u];i!=-1;i=edge2[i].next)
{
int v = edge2[i].to;
in[v]--;
if(!in[v])
{
q.push(v);
c++;
}
}
if(c>1)
return false;
}
return true;
}
int main()
{
int m,a,b,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
xx.clear();
cnt = 0;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
xx.push_back(make_pair(a,b));
}
tarjan();
memset(head2,-1,sizeof(head2));
cnt2 = 0;
for(int i=0;i<xx.size();i++)
{
int u = xx[i].first,v = xx[i].second;
if(belong[u]!=belong[v])
{
add2(belong[u],belong[v]);
in[belong[v]]++;
}
}
if(topo()) printf("Yes\n");
else printf("No\n");
}
return 0;
}
Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117526.html Link to the original text :https://javaforall.cn
边栏推荐
- VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
- 【经典控制理论】自控实验总结
- audiopolicy
- 媒体查询:引入资源
- C Primer Plus Chapter 9 question 9 POW function
- Openresty ngx Lua regular expression
- LeetCode——Add Binary
- Nacos installation and service registration
- Realize reverse proxy client IP transparent transmission
- Non rigid / flexible point cloud ICP registration
猜你喜欢
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Creative mode 1 - single case mode
2:第一章:认识JVM规范1:JVM简介;
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Getting started stm32--gpio (running lantern) (nanny level)
TypeError: this. getOptions is not a function
东南亚电商指南,卖家如何布局东南亚市场?
随机推荐
Debian 10 installation configuration
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
Error when LabVIEW opens Ni instance finder
Krypton Factor purple book chapter 7 violent solution
C Primer Plus Chapter 9 question 9 POW function
openresty ngx_ Lua request response
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
Alibaba Tianchi SQL training camp task4 learning notes
【Note17】PECI(Platform Environment Control Interface)
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
One article deals with the microstructure and instructions of class
Non rigid / flexible point cloud ICP registration
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
Using LNMP to build WordPress sites
Hcip day 12 (BGP black hole, anti ring, configuration)
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
Multi camera stereo calibration
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Three.js-01 入门
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)