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 <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
const int N = 100005;
const int M = 105;
const ll mod = 1000000009;

using namespace std;

int n;
vector <int> G[N];
int dfs_clock, bcc_cnt, pre[N], iscut[N], bcc[N];

int dfs(int u, int 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] == 0) {
			child++;
			int lowv = dfs(v, u);
			lowu = min(lowu, lowv);
			if(lowv >= pre[u]) {
				iscut[u] = 1;
			}
		} else if(pre[v] < pre[u] && v != fa) {
			lowu = min(lowu, pre[v]);
		}
	}
	if(fa < 0 && child == 1) {
		iscut[u] = 0;
	}
	return lowu;
}

void find_bcc() {
	mem(pre);
	mem(iscut);
	mem(bcc);
	dfs_clock = bcc_cnt = 0;
	for(int i = 0; i < n; i++) {
		if(pre[i] == 0) {
			dfs(i, -1);
		}
	}
}

int main() {

//	freopen("in.txt", "r", stdin);
	while(cin >> n, n) {
		getchar();
		for(int i = 0; i < n; i++) {
			G[i].clear();
		}
		string line;
		while(getline(cin, line)) {
			if(line == "0") break;
			int x;
			stringstream ss(line);
			int ok = 1, u;
			while(ss >> x) {
				x--;
//				printf("%d ", x+1);
				if(ok) {
					u = x;
					ok = 0;
				} else {
					G[u].push_back(x);
					G[x].push_back(u);
				}
			}

		}
//		puts("");
//		for(int i = 0; i < n; i++) {
//			printf("%d : ", i+1);
//			for(int j = 0; j < G[i].size(); j++) {
//				printf("%d ", G[i][j] + 1);
//			}
//			puts("");
//		}
		find_bcc();
		int ans = 0;
		for(int i = 0; i < n; i++) {
			if(iscut[i]) ans++;
		}
		printf("%d\n", ans);

	}

	return 0;
}

UVA - 10199

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
const int N = 105;
const int M = 10005;
const ll mod = 1000000009;

using namespace std;

int n, m;
vector <int> G[N];
int dfs_clock, bcc_cnt, pre[N], iscut[N], bcc[N];

int dfs(int u, int 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] == 0) {
			child++;
			int lowv = dfs(v, u);
			lowu = min(lowu, lowv);
			if(lowv >= pre[u]) {
				iscut[u] = 1;
			}
		} else if(pre[v] < pre[u] && v != fa) {
			lowu = min(lowu, pre[v]);
		}
	}
	if(fa < 0 && child == 1) {
		iscut[u] = 0;
	}
	return lowu;
}

void find_bcc() {
	mem(pre);
	mem(iscut);
	mem(bcc);
	dfs_clock = bcc_cnt = 0;
	for(int i = 0; i < n; i++) {
		if(pre[i] == 0) {
			dfs(i, -1);
		}
	}
}

int main() {

		//freopen("in.txt", "r", stdin);
		int ca = 1;
		while(cin >> n) {
			if(n == 0) break;

			for(int i = 0; i < n; i++) {
				G[i].clear();
			}
			map<string, int> mp;
			string s, t, in[N];
			for(int i = 0; i < n; i++) {
				cin >> in[i];
				mp[in[i]] = i;
			}
			cin >> m;
			for(int i = 0; i < m; i++) {
				cin >> s >> t;
				int x = mp[s];
				int y = mp[t];
				G[x].push_back(y);
				G[y].push_back(x);
			}

			find_bcc();

			int ans = 0;
			set<string> st;
			for(int i = 0; i < n; i++) {
				if(iscut[i]) {
					st.insert(in[i]);
					ans++;
				}
			}

			if(ca > 1) puts("");
			printf("City map #%d: %d camera(s) found\n", ca++, ans);
			for(set<string>::iterator it = st.begin(); it != st.end(); ++it) {
				cout << *it << endl;
			}

		}

	return 0;
}
时间: 2024-12-15 06:50:29

UVA - 315 Network 和 UVA - 10199 (求割顶)的相关文章

无向图求割顶与桥

无向图求割顶与桥 对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶.对于连通图,割顶就是删除之后使图不再连通的点.如果删除边(u,v)一条边,就可以让连通图变成不连通的,那么边(u,v)是桥. 具体的概念和定义比较多,在刘汝佳<<训练指南>>P312-314页都有详细的介绍. 下面来写求无向图割顶和桥的DFS函数.我们令pre[i]表示第一次访问i点的时间戳,令low[i]表示i节点及其后代所能连回(通过反向边)的最早祖先的pre值. 下面的dfs函数返回

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

uva 315 Network(连通图求割点)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251  Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers

UVA 315 Network(无向图求割点)

题目大意 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 together two pl

POJ 1144 &amp; Uva 315 Network 【求割点数目】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10855   Accepted: 5020 链接:http://poj.org/problem?id=1144 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places

UVA - 315 Network(tarjan求割点的个数)

题目链接:https://vjudge.net/contest/67418#problem/B 题意:给一个无向连通图,求出割点的数量.首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第一个数字代表后面的都和它存在边,0表示行输入的结束. 题解:简单的求割点模版,所谓割点就是去掉这一个点还有于这个点链接的边之后使得原来的图连通块增加. 由于这是模版题代码会加上注释. #include <iostream> #include <cstring> using namesp

无向图求割点 UVA 315 Network

输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <stack> #include <cstring> using namespa

UVA 315 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 togethe

Uva 315 Network 判断割点

模板题,注意输出 #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <vector> #include <stack> using namespace std; typedef long long LL; const int N = 1e2+5; int head[N],tot,n; struct Edge{ i