uoj#67 新年的毒瘤【Tarjan】

时间:2023-03-09 19:08:34
uoj#67 新年的毒瘤【Tarjan】

题目http://uoj.ac/problem/67

题意:n个节点m条边的图,删除某个节点及他相连的所有边之后,剩下的图就成了一棵树。找出所有这样的节点。

思路:上次去清华面试的B题,当时就是在瞎写。太菜了。

其实就是两点,一是删除一个点是树,那么剩下来只有$n-2$条边。也就是说删掉的那个点的度是$m - (n - 2)$

其次,删掉了这个点之后还需要保证图的连通性,也就是说割点一定不是。

所以就先用Tarjan求出所有割点,再找不是割点且度是$m - (n - 2)$的点。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n, m;
const int maxn = 1e5 + ;
struct edge{
int to, nxt;
}e[maxn * ];
int tot = , head[maxn], dfn[maxn], low[maxn]; void add(int x, int y)
{
e[++tot].to = y;
e[tot].nxt = head[x];
head[x] = tot;
e[++tot].to = x;
e[tot].nxt = head[y];
head[y] = tot;
} int num, root;
bool cut[maxn];
void tarjan(int x)
{
dfn[x] = low[x] = ++num;
int flag = ;
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(!dfn[y]){
tarjan(y);
low[x] = min(low[x], low[y]);
if(low[y] >= dfn[x]){
flag++;
if(x != root || flag > )cut[x] = true;
}
}
else low[x] = min(low[x], dfn[y]);
}
} int deg[maxn];
int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++){
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
deg[x]++;deg[y]++;
}
for(int i = ; i <= n; i++){
if(!dfn[i]){
root = i;
tarjan(i);
}
}
vector<int>ans;
for(int i = ; i <= n; i++){
if(!cut[i] && deg[i] == m - (n - ))ans.push_back(i);
}
printf("%d\n%d", ans.size(), ans[]);
for(int i = ; i < ans.size(); i++){
printf(" %d", ans[i]);
}
printf("\n");
return ;
}

---恢复内容结束---