[USACO06JAN]冗余路径Redundant Paths 无向图tarjan缩点

Posted wangsheng5

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO06JAN]冗余路径Redundant Paths 无向图tarjan缩点相关的知识,希望对你有一定的参考价值。

如题,缩完点后数一下有几个入度为1的scc,+1再/2即可。

教训:加一个cntf处理重边!否则重边会被认为是同一条。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 
 8 struct stack{
 9     vector<int> v;
10     void push(int x){v.push_back(x);}
11     void pop(){v.pop_back();}
12     int size(){return v.size();}
13     bool empty(){return v.empty();}
14     int top(){return v[v.size()-1];}
15 }s;
16 
17 const int Maxn = 5010;
18 
19 vector<int> g[Maxn],scc[Maxn];
20 int dfn[Maxn],low[Maxn],inscc[Maxn],ins[Maxn];
21 int inv[Maxn];
22 int n,m,cntv,cntscc,cnt;
23 
24 void tarjan(int x,int fa){
25     low[x] = dfn[x] = ++cntv,ins[x] = 1;
26     s.push(x);int cntf = 0;
27     for(int i = 0;i < g[x].size();i++){
28         int u = g[x][i];
29         if(u == fa&&!cntf){cntf++;continue;}
30         if(!dfn[u])tarjan(u,x),low[x] = min(low[x],low[u]);
31         else low[x] = min(low[x],dfn[u]);
32     }
33     if(dfn[x] == low[x]){
34         int y = -1;cntscc++;
35         while(!s.empty()&&y^x){
36             y = s.top();s.pop();
37             ins[y] = 0,inscc[y] = cntscc;
38             scc[cntscc].push_back(y);
39         }
40     }
41 }
42 
43 int main(){
44     scanf("%d%d",&n,&m);
45     for(int i = 1;i <= m;i++){
46         int u,v;
47         scanf("%d%d",&u,&v);
48         g[u].push_back(v);
49         g[v].push_back(u);
50     }
51     tarjan(1,-1);
52     for(int i = 1;i <= n;i++)
53         for(int j = 0;j < g[i].size();j++)
54             if(inscc[i]^inscc[g[i][j]])inv[inscc[g[i][j]]]++;
55     for(int i = 1;i <= cntscc;i++)if(inv[i] == 1)cnt++;
56     cout << (cnt+1)/2;
57 return 0;
58 }

 

以上是关于[USACO06JAN]冗余路径Redundant Paths 无向图tarjan缩点的主要内容,如果未能解决你的问题,请参考以下文章

luogu P2860 [USACO06JAN]冗余路径Redundant Paths |Tarjan

[USACO06JAN]冗余路径Redundant Paths(缩点)

洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求双联通分量)

[USACO06JAN]冗余路径Redundant Paths 无向图tarjan缩点

LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

poj——3177Redundant Paths