BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径

给你一个无向图,问至少加几条边可以使整个图变成一个双联通分量

简单图论练习= =

先缩点,ans = (度数为1的点的个数) / 2

这不是很好想的么QAQ

然后注意位运算的优先级啊魂淡!!!你个sb调了一个下午!!!

 /**************************************************************
Problem: 1718
User: rausen
Language: C++
Result: Accepted
Time:44 ms
Memory:3148 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 5e4 + ;
const int M = 1e5 + ; struct edge {
int next, to;
edge(int _n = , int _t = ) : next(_n), to(_t) {}
} e[M]; int n, m, ans;
int cnt, top, num, tot = ;
int dfn[N], low[N], vis[N], sz[N], s[N], w[N], first[N];
int mp[N]; inline void Add_Edges(int x, int y) {
e[++tot] = edge(first[x], y), first[x] = tot;
e[++tot] = edge(first[y], x), first[y] = tot;
} void DFS(int p, int from) {
dfn[p] = low[p] = ++cnt;
s[++top] = p, vis[p] = ;
#define y e[x].to
register int x;
for (x = first[p]; x; x = e[x].next)
if (x != (from ^ )) {
if (!vis[y]) DFS(y, x);
if (vis[y] < ) low[p] = min(low[p], low[y]);
}
#undef y
if (dfn[p] == low[p]) {
register int y;
++num;
while (s[top + ] != p) {
y = s[top--];
vis[y] = , w[y] = num;
++sz[num];
}
}
} inline void tarjan() {
int i;
cnt = top = num = ;
memset(vis, sizeof(vis), );
for (i = ; i <= n; ++i)
if (!vis[i]) DFS(i, );
} int main() {
int i, x, y;
scanf("%d%d", &n, &m);
for (i = ; i <= m; ++i) {
scanf("%d%d", &x, &y);
Add_Edges(x, y);
}
tarjan();
#define y e[x].to
memset(mp, , sizeof(mp));
for (i = ; i <= n; ++i)
for (x = first[i]; x; x = e[x].next)
if (w[i] != w[y]) ++mp[w[y]];
for (ans = i = ; i <= num; ++i)
ans += (mp[i] == );
printf("%d\n", ans >> );
#undef y
return ;
上一篇:Python爬虫实战五之模拟登录淘宝并获取所有订单


下一篇:Python爬虫实战---抓取图书馆借阅信息