标签:des style blog class code c
Description
Input
Output
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
2
Hint
1 2 3Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +
1 2 3Check some of the routes:
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; int degree[5000+10]; struct CUT_E { static const int maxn=5000+10; int low[maxn],pre[maxn],dfs_clock,n,m,sumcut; int cut_edge[maxn][maxn]; vector<int>group[maxn]; void init() { for (int i=0;i<=n;i++) { group[i].clear(); for (int j=0;j<=n;j++) cut_edge[i][j]=0; } sumcut=0; dfs_clock=0; } void addedge(int u,int v) { group[u].push_back(v); group[v].push_back(u); } int dfs(int u,int fa) { int lowu=pre[u]=++dfs_clock; for (int i=0;i<group[u].size();i++) { int v=group[u][i]; if (!pre[v]) { int lowv=dfs(v,u); lowu=min(lowu,lowv); if (lowv>pre[u]) {cut_edge[u][v]=1;cut_edge[v][u]=1;} } else if (pre[v]<pre[u] && v!=fa) lowu=min(lowu,pre[v]); } low[u]=lowu; return lowu; } int get_sum() { int ans=dfs(-1,1); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (cut_edge[i][j]) sumcut++; return sumcut; } }; CUT_E stable; void update() { for (int i=0;i<=stable.n;i++) stable.group[i].clear(); for (int i=1;i<=stable.n;i++) for (int j=1;j<=stable.n;j++) if (stable.cut_edge[i][j]==2) { stable.group[i].push_back(j); stable.group[j].push_back(i); } } bool vis[5000+10]; int p[5000+10]; void DFS(int u,int fa) { p[u]=fa; for (int i=0;i<stable.group[u].size();i++) { int v=stable.group[u][i]; if(!vis[v]) { vis[v]=true; DFS(v,fa); } } } void solve() { for (int i=1;i<=stable.n;i++) for (int j=1;j<=stable.n;j++) if (stable.cut_edge[i][j]==1) { int x=p[i]; int y=p[j]; degree[x]++; degree[y]++; } } int find_leaf() { int ans=0; for (int i=1;i<=stable.n;i++) if (degree[i]==2) ans++; return ans; } int main() { //freopen("in.txt","r",stdin); int f,r,x,y; while (scanf("%d%d",&f,&r)!=EOF) { stable.n=f; stable.m=r; stable.init(); for (int i=1;i<=r;i++) { scanf("%d%d",&x,&y); stable.addedge(x,y); stable.cut_edge[x][y]=2; stable.cut_edge[y][x]=2; } int temp=stable.dfs(1,-1);//求割边 update();//去掉割边,更新图 memset(vis,0,sizeof(vis));vis[1]=true; for (int i=1;i<=f;i++)//找出每一个连通快,缩点 if (!vis[i] || i==1)DFS(i,i); memset(degree,0,sizeof(degree)); solve(); int leaf=find_leaf();//找度为一的节点 printf("%d\n",(leaf+1)/2); } return 0; }
由于我用矩阵来更新度,故度为2的点才是
poj 3177 Redundant Paths,布布扣,bubuko.com
标签:des style blog class code c
原文地址:http://www.cnblogs.com/chensunrise/p/3735505.html