意甲冠军:给定一个有向图有m单向边缘。免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求

弱联通重量。

算法:

缩点求强连通分量。然后又一次建图。推断新图是否是一条单链,即不能分叉,假设分叉了就会存在不可达的情况。

怎么推断是否是单链呢?

就是每次入度为0的点都仅仅有一个,即每次队列里仅仅有一个点。

(    o(╯□╰)o。。。。

。好像已经是第二次用pair记录原图的点对,然后存pair的vector忘记清空导致wa来wa去!

#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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)的更多相关文章

  1. 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. ...

  2. 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:  ...

  3. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  4. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  5. [ tarjan + dfs ] 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 L ...

  6. POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...

  7. [强连通分量] 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 ...

  8. 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: 15812 ...

  9. POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17104   Accepted: 4594 Description In o ...

随机推荐

  1. WASP_百度百科

    WASP_百度百科 WASP

  2. ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;&amp;prim)

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  3. 关于在ios7之后改变状态栏颜色

    看到网上都说 在ios7之后要这样设置 首先,须要在Info.plist配置文件里,添加键:UIViewControllerBasedStatusBarAppearance,并设置为YES: 然后,在 ...

  4. Projecet客户端登陆无法通过验证

    客户反映使用Project客户端登陆project服务器的时候,只有域管理员账户才能够登陆成功,其他的账户登陆都无法验证通过,无论是https的方式还是客户端的方式,但是域账户登陆计算机是可以登陆成功 ...

  5. const void *a 与 void *const a 的差别

    const void *a 这是定义了一个指针a,a能够指向随意类型的值,但它指向的值必须是常量. 在这样的情况下,我们不能改动被指向的对象,但能够使指针指向其它对象. 比如: const void ...

  6. Redis设计与实现读书笔记——双链表

    前言 首先,贴一下参考链接: http://www.redisbook.com/en/latest/internal-datastruct/adlist.html, 另外真赞文章的作者,一个90后的小 ...

  7. WPF界面设计技巧(8)—自制山寨版CheckListBox

    原文:WPF界面设计技巧(8)-自制山寨版CheckListBox 近年来IT市场山寨横行啊,我们今天也来发扬一下山寨精神,搞个自制的CheckListBox出来. 喏,CheckListBox 就是 ...

  8. libgdx如何调用android平台内容

    使用libgdx已经有一段时间了.最近经常有朋友问我如何在libgdx中调用android的内容. 正常来说libgdx是跨平台的,gdx中的代码是不允许有任何其他平台的相关代码,但实际使用时经常会有 ...

  9. 怎样在屏幕上显示多个alv

    本文解说怎样在屏幕上显示多个alv. 实现这种需求关键是下面几点(举例:在屏幕上显示4个alv): 1.须要定义4个alv control 2.由于有4个alv control,于是就须要定义4个容器 ...

  10. JavaWeb学习总结(一)JavaWeb开发入门

    静态网页和动态网页 静态网页:在服务器上没有经过服务器解释执行的网页. 动态网页:在服务器上经过服务器解释执行的网页. 无论是静态网页还是动态网页,客户端看到的网页都是由HTML所构成的,所以Java ...