uva 10765 Doves and Bombs(割顶)

??

题意:给定一个n个点的连通的无向图,一个点的“鸽子值”定义为将它从图中删去后连通块的个数。求每一个点的“鸽子值”。

思路dfs检查每一个点是否为割顶,并标记除去该点后有多少个连通分量

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std;  

const int maxn = 10000 + 100;
const int INF = 0x3f3f3f3f;
int n, m;
vector<int> G[maxn];
int val[maxn], node[maxn]; //node数组记录结点id间接排序
bool cmp(int x, int y) {
	return val[x] == val[y] ? x < y : val[x] > val[y];
}

int pre[maxn], dfs_clock;
int dfs(int u, int fa) { //u在dfs树中的父节点为fa
	int lowu = pre[u] = ++dfs_clock;
	int child = 0;        //子节点个数
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if(!pre[v]) {   //没有訪问过v
			child++;
			int lowv = dfs(v, u);
			lowu = min(lowu, lowv);    //用后代的low函数更新u的low函数
			if(lowv >= pre[u]) {
				val[u]++;
			}

		}
		else if(pre[v] < pre[u] && v != fa)  lowu = min(lowu, pre[v]);  //用反向边更新u的low函数
	}
	if(fa < 0 && child == 1) val[u] = 1;
	return lowu;
} 

void init() {
	dfs_clock = 0;
	memset(pre, 0, sizeof(pre));
	for(int i = 0; i < n; i++) {
		G[i].clear();
		node[i] = i;
		val[i] = 1;
	}
	int x, y;
	while(scanf("%d%d", &x, &y) == 2 && x >= 0) {
		G[x].push_back(y);
		G[y].push_back(x);
	}
}

void solve() {
	dfs(0, -1);
	sort(node, node+n, cmp);
	for(int i = 0; i < m; i++) cout << node[i] << " " << val[node[i]] << endl;
	cout << endl;
}

int main() {
	//freopen("input.txt", "r", stdin);
	while(scanf("%d%d", &n, &m) == 2 && n) {
		init();
		solve();
	}
	return 0;
}
时间: 2024-10-29 05:25:09

uva 10765 Doves and Bombs(割顶)的相关文章

UVA - 10765 Doves and bombs (双联通分量)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34798 给N个点的无向图并且联通,问删除每次一个点之后还剩多少联通分量. 找割顶 如果删除的是割顶 联通分量就会增加,否则还是1(因为原图是联通图),删除割顶之后 联通块的数目 就要看该割顶在几个双联通分量里出现过. #pragma comment(linker, "/STACK:10240000,10240000") #include <a

UVA 10765 Doves and bombs 割点

最近好懒,堆了好多题没写题解.. 原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1706 题意: 给你一个图,问你每个点去掉后有多少个联通块 题解: 就Tarjan一下就好,很简单 代码: #include<iostream> #include<cstring> #include<vector>

10765 - Doves and bombs(双连通分量)

UVA 10765 - Doves and bombs 题目链接 题意:给定一个无向图,每个点的鸽子值为删去这个点后会有几个连通块,问鸽子值前m大的点的鸽子值,如果相同,按编号排 思路:就裸的双连通分量,在每个连通分量如果是割顶的点就加一,最后如果答案为0的点,答案应该是1 代码: #include <cstdio> #include <cstring> #include <vector> #include <stack> #include <algo

UVA - 315 Network 和 UVA - 10199 (求割顶)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21278 求割顶的裸题. UVA - 315 #include <algorithm> #include <iostream> #include <sstream> #include <cstrin

UVA 315 :Network (无向图求割顶)

题目链接 题意:求所给无向图中一共有多少个割顶 用的lrj训练指南P314的模板 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=109; struct Edge { int to,next; Edge(){} Edge(int _to,int _next) { to=_to; next=_next; } }edge[N*N*2]; int head[N]; int dfn[N],lo

hdu 4587(枚举+割顶)

TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2354    Accepted Submission(s): 780 Problem Description Suppose that G is an undirected graph, and the value of stab is defined as fol

图论(无向图的割顶):POJ 1144 Network

Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect

【模板】无向图的割顶

无向图的割顶: Vector <int> G[] :邻接表存图 Int pre[] :存储时间戳 Int low[] : u及其后代所能连回的最早的祖先的pre值 Int iscut[] : =true表示是割顶,=false不是割顶 Dfs函数在主函数调用时,fa预设为-1. vector <int> G[MAXN]; int pre[MAXN],iscut[MAXN],low[MAXN],dfs_clock; int dfs(int u,int fa) { int lowu=p

找割顶的模板

用邻接矩阵写的.自己慢慢理解吧 #include <iostream> #include <cstring> using namespace std; #define CC(c) memset(c, 0, sizeof(c)) const int maxn=5000; int iscut[maxn], g[maxn][maxn], low[maxn], pre[maxn], _pre, n, m; int dfs(int u, int fa) { low[u]=pre[u]=++_