POJ3352(题目加题解加数据加造数据代码)

文章目录

题目:

题目链接点这里
翻译:
道路建设
时限:2000 MS 内存限制:65536K
提交材料共计: 13799 接受: 6969
描述

这几乎是夏天的时间,这意味着它几乎是夏季建筑时间!今年,负责偏远岛屿热带岛屿天堂道路的好心人希望修复和升级岛上各旅游景点之间的各种道路。

道路本身也相当有趣。由于岛上的奇怪习俗,道路的布置使它们永远不会在交叉口相遇,而是通过桥梁和隧道相互通过或在彼此下面通过。这样,每条路都会在两个特定的旅游景点之间行驶,这样游客就不会失去不可挽回的损失。

不幸的是,鉴于每条道路所需维修和升级的性质,当建筑公司在一条特定的道路上施工时,它在任何一个方向都无法使用。如果不可能在两个旅游景点之间旅行,即使建筑公司在任何特定时间只在一条道路上施工,也会造成问题。

因此,偏远岛屿公路部决定请您的咨询服务来帮助解决这个问题。当局已决定,须在各景点之间兴建新道路,以便在最后布置时,如有任何一条道路正在兴建,则仍可使用余下的道路在任何两个旅游景点之间行走。你的任务是找到所需的最少数量的新道路。

输入

第一行输入将由正整数组成。n和r,由一个空格分隔,其中3≤n≤1000是岛上旅游景点的数量,2≤r≤1000是道路的数量。旅游景点在1到1之间贴上了方便的标签。n。以下每一项r行将由两个整数组成,v和w,由一个空间隔开,表示有一条路存在于标有标志的景点之间。v和w。请注意,你可以沿着每条路往任何一个方向走,任何一对旅游景点最多只能有一条直接在它们之间的路。此外,您可以放心,在目前的配置,是可以旅行之间的任何两个旅游景点。

输出量

一行,由一个整数组成,它给出了我们需要添加的最小道路数。

样本输入

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
样本输出

Output for Sample Input 1
2

Output for Sample Input 2
0
数据:
先别急着看题解,先来几组数据先
输入1:
11 14
1 2
1 3
1 4
2 5
6 11
2 6
5 6
5 11
3 7
3 8
7 8
4 9
4 10
9 10

11 14
1 2
1 3
1 4
5 11
2 5
2 6
5 6
6 11
3 7
3 8
7 8
4 9
4 10
9 10
输出1:
1
2

输入2:
5 5
1 2
1 3
1 4
1 5
2 3

9 9
2 3
3 4
1 5
4 5
6 4
9 5
1 4
1 8
1 6
输出2:
1
2
输入3:
50 50
42 18
35 1
20 25
29 9
13 15
6 46
32 28
12 42
46 43
28 37
42 5
3 4
43 33
22 17
19 46
48 27
22 39
20 13
18 50
36 45
4 12
23 34
24 15
42 12
4 19
48 45
13 8
38 10
24 42
30 29
17 36
41 43
39 7
41 43
15 49
47 6
41 30
21 1
7 2
44 49
30 24
35 5
7 41
17 27
32 9
45 40
27 24
38 39
19 33
30 42
输出3
6
如果还需要数据的话可以自己造
代码如下:

#include<cstdio>//点个赞吧!!~~~QWQ
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<time.h>
#define asdasd
using namespace std;
int main(){
	#ifdef asdasd
		freopen("test.in","r",stdin);
		freopen("test.out","w",stdout);
		#endif
	int n,m;
	scanf("%d %d",&n,&m);
	printf("%d %d\n",n,m);
	for(register int i=1;i<=m;){
		int a,b;
		a=1+rand()%n;
		b=1+rand()%n;
		if(a!=b){
			printf("%d %d\n",a,b);
			i++;
		}
	}
	return 0;
}

至于怎么使用的话就不讲了

题解

明明感觉是到模板题,但是我却过的这么艰难。。。。哎╮(╯▽╰)╭
坑点:样例输入里的“Sample Input 1”没有任何作用。。。。。坑~~

AC代码:

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
vector<int > s[1006];
int dfsn[1006],low[1006];
int tot;
void tarjan(int u,int fa){
	dfsn[u]=low[u]=++tot;
	for(register int i=0;i<s[u].size();++i){
		int v=s[u][i];
		if(v==fa)continue;
		if(!dfsn[v])tarjan(v,u);
		low[u]=min(low[u],low[v]);
	}
}

int main(){
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		memset(s,0,sizeof(s));
		memset(dfsn,0,sizeof(dfsn));
		memset(low,0,sizeof(low));
		tot=0;
		while(m--){
			int x,y;
			scanf("%d %d",&x,&y);
			s[x].push_back(y);
			s[y].push_back(x);
		}
		tarjan(1,-1);
		int tmp[1006];
		memset(tmp,0,sizeof(tmp));
		for(register int i=1;i<=n;++i)
			for(register int j=0;j<s[i].size();++j){
				int v=s[i][j];
				if(low[i]!=low[v])++tmp[low[i]];
			}
		int ans=0;
		for(register int i=1;i<=n;++i)
			if(tmp[i]==1)ans++;
		printf("%d\n",(ans+1)/2);	
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值