我是个没有感情的敲板子机器
(一)无向图的连通性
A - Network POJ - 1144
题目描述
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 places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
Input
The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
Output
The output contains for each block except the last in the input file one line containing the number of critical places.
Sample Input
5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0
Sample Output
1
2
Hint
You need to determine the end of one line.In order to make it’s easy to determine,there are no extra blank before the end of each line.
理解
这是一个无向图,先给定n表示一共有n个结点
接下来 每一行 第一个数表示一个结点u,后面的为他的子结点vi
然后u和vi之间有线连通,当开头为0时表示输入结束,
直接求割点然后套Tarjan模板就好了,或许数据读入处理有点小麻烦(并不)
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10010;
struct node{
int to,next;
}edge[maxn];
int head[maxn],tot;
int low[maxn],dfn[maxn];
int index,root;
bool iscut[maxn];
bool cut[maxn];
void add(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int u,int pre){
int v;
low[u] = dfn[u] = ++index;
iscut[u] = true;
int son = 0;
for(int i = head[u];i != -1;i = edge[i].next){
v = edge[i].to;
if(v == pre) continue;
if(!dfn[v]){
tarjan(v,u);
son++;
if(low[u] > low[v]) low[u] = low[v];
if(u == root && son > 1) cut[u] = true;
if(u != root && low[v] >= dfn[u]){
cut[u] = true;
}
}
else low[u] = min(dfn[v],low[u]);
}
}
int main(){
int ans,n;
while(~scanf("%d",&n) && n){
memset(head,-1,sizeof(head));
memset(cut,false,sizeof(cut));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
index = 0;
tot = 0;
root = 1;
int x,y;
while(cin >> x && x){
while(getchar() != '\n'){
cin >> y;
add(x,y);
add(y,x);
}
}
tarjan(1,-1);
int ans = 0;
for(int i = 1;i <= n;i++) if(cut[i]) ans++;
printf("%d\n",ans);
}
return 0;
}
B - Road Construction POJ - 3352
题目描述
It’s almost summer time, and that means that it’s almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10
Sample Input 2
3 3
1 2
2 3
1 3
Sample Output
Output for Sample Input 1
2
Output for Sample Input 2
0
理解
给出若干个点之间的几条路,每条路给定长度和花费
在保证点1到其他点距离最短的情况下的最少花费
我是把这题当做floyd求最小闭环来做的
网上好多大佬用的spfa和dijkstra 做松弛操作来写的
floyd的实现过程,枚举顶点k前已经获得了顶点为1 - k-1 的最短路,
在更新k前枚举i和j,可以知道dis[i][j]没有经过k点,来知道
如果dis[i][j]+mp[i][k]+mp[k][j] != inf(mp[i][j]为没有更新边值) 时
那么就存在一条经过ijk的最小环
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 10;
struct node{
int to,next;
}edge[maxn];
int head[maxn],tot;
int low[maxn],dfn[maxn];
int top,index;
void add(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int u,int pre){
int v;
low[u] = dfn[u] = index++;
for(int i = head[u];i != -1;i = edge[i].next){
v = edge[i].to;
if(dfn[v] == -1){
tarjan(v,u);
if(low[u] > low[v]) low[u] = low[v];
}
else if(v != pre){
if(low[u] > low[v]) low[u] = low[v];
}
}
}
int solve(int n){
int v,le = 0;
int de[maxn];
memset(de,0,sizeof(de));
for(int i = 0;i < n;i++){
for(int j = head[i];j != -1;j = edge[j].next){
v = edge[j].to;
if(low[i] != low[v]){
de[low[i]]++;
de[low[v]]++;
}
}
}
for(int i = 0;i < n;i++)
if(de[i] == 2) le++;
return (le+1)/2;
}
int main(){
int n,r;
scanf("%d %d",&n,&r);
memset(head,-1,sizeof(head));
memset(low,-1,sizeof(low));
memset(dfn,-1,sizeof(dfn));
top = index = tot = 0;
for(int i = 0;i < r;i++){
int a,b; scanf("%d %d",&a,&b);
add(a-1,b-1); add(b-1,a-1);
}
tarjan(0,0);
printf("%d\n",solve(n));
return 0;
}
留一个floyd最小环的板子
https://blog.csdn.net/u012534831/article/details/74231581
(二)有向图的连通性
A - 迷宫城堡 HDU - 1269
题目描述
为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i。
Input
输入包含多组数据,输入的第一行有两个数:N和M,接下来的M行每行有两个数a和b,表示了一条通道可以从A房间来到B房间。文件最后以两个0结束。
Output
对于输入的每组数据,如果任意两个房间都是相互连接的,输出"Yes",否则输出"No"。
Sample Input
3 3
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0
Sample Output
Yes
No
理解
其实这题有很多解法,,floyd或者tarjan,然后还听了大佬邻接表+队列的写法
然后我毅然决然选择了一手dfs
AC代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n,m;
vector<int> v[maxn];
bool f[maxn] = {false};
void dfs(vector<int> v[],int t){
f[t] = true;
for(int i = 0;i < v[t].size();i++)
if(f[v[t][i]]==false)
dfs(v,v[t][i]);
}
int main(){
while(~scanf("%d %d",&n,&m)){
if(n == 0 && m == 0) break;
bool flag = true;
for(int i = 0;i < m;i++){
int a,b; scanf("%d %d",&a,&b);
v[a].push_back(b);
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++)
f[j] = false;
dfs(v,i);
for(int j = 1;j <= n;j++){
if(f[j]==false){
flag = false;
break;
}
}
if(!flag) break;
}
if(flag) printf("Yes\n");
else printf("No\n");
for(int i = 1;i <= n;i++)
v[i].clear();
}
return 0;
}
(三)我的感想(布星)
关于这周的日常练习
这周运动会放假,umm偷懒跑出去玩勒,主要是这个有向图无向图的连通真的不是我的菜,感觉打打板子海星,稍微变通一下就需要别人挑明一下,有点难想,好不容易会了点tarjan的板子,感觉邻接表比链式前向星用起来顺手,可能是因为我还没完全理清链式前向星的逻辑关系吧,上周咕咕了一次csdn,选择了手写板子记在书上,的确有效,然后这周就懒得手写了,敲敲键盘他不香吗(明明是自己犯懒没学新东西)