跳过正文
  1. Posts/

codeforces 217A ice skating

·1 分钟

http://codeforces.com/problemset/problem/217/A 题意:有n个雪漂(那是啥?,从某个雪漂出发走直线,只有到达另一个雪飘才能停下来。问最少需要添加多少个雪漂,才能使得可以到达任何一个雪漂。 思路:横坐标相同或者纵坐标相同的两个点之间是可以到达的。先O(N2)扫一遍建图。记录这个森林中数的个数为cnt,cnt-1即为答案。因为对于任意两个不能相互到达的点。我们只需要再来一个雪漂就可以使得这两个点相互到达。

一遍AC,有点爽。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月05日 星期六 20时18分23秒
 4File Name :code/problem/217A.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int n;
35bool vis[N];
36int x[N],y[N];
37
38vector<int>edge[N];
39
40void dfs( int x)
41{
42    vis[x] = true;
43    for ( int i = 0 ; i < edge[x].size() ; i++)
44    {
45	int v = edge[x][i];
46	if  (!vis[v])
47	{
48	    dfs(v);
49	}
50    }
51}
52int main()
53{
54	#ifndef  ONLINE_JUDGE
55	freopen("code/in.txt","r",stdin);
56  #endif
57
58	scanf("%d",&n);
59	for ( int i = 0 ; i < n ; i++) scanf("%d %d",&x[i],&y[i]);
60
61	for ( int i = 0 ; i  < n-1 ; i++)
62	{
63	    for ( int j = i+1 ; j < n ; j++)
64	    {
65		if (x[i]==x[j])
66		{
67		    edge[i].push_back(j);
68		    edge[j].push_back(i);
69		}
70		if (y[i]==y[j])
71		{
72		    edge[i].push_back(j);
73		    edge[j].push_back(i);
74		}
75
76	    }
77	}
78	ms(vis,false);
79	int cnt = 0 ;
80	for ( int i = 0 ; i < n ; i++)
81	{
82	    if (!vis[i])
83	    {
84		dfs(i);
85		cnt++;
86	    }
87	}
88	cout<<cnt-1<<endl;
89
90  #ifndef ONLINE_JUDGE
91  fclose(stdin);
92  #endif
93    return 0;
94}

相关文章

codeforces 445 B. DZY Loves Chemistry

·2 分钟
http://codeforces.com/contest/445/problem/B 题意:一共有n种化学药品。m对关系,每对关系表示为x,y表示x和y相互反应。初始容器的danger值为1,当向容器中加入一个化学药品A,如果容器中存在化学药品和A反应,那么容器的danger值翻倍。否则不变。问一个最优的放置药品的顺序。

codeforces 580 C. Kefa and Park

·1 分钟
http://codeforces.com/contest/580/problem/C 题意:给出一棵树。每个叶子节点上有一个饭店。某些节点上有cat.现在问从根节点出发可以到达多少个饭店,保证在到达饭店的路径中补连续遇到m个以上的cat.

codeforces 115A A. Party

·1 分钟
http://codeforces.com/problemset/problem/115/A 题意:给出n个人之间的上级下级关系。问如何分得最少的组,使得没一组中的人不存在上下级关系。 思路:用树的观点来考虑会很容易。可以看成给了一棵森冷。对于不同的树的相同层的点,不存在上下级关系,可以放在一个group.对于同一棵树,每一层要单独放一个group.所以答案是所有树的深度的最大值。

codeforces 522 A. Reposts

·1 分钟
http://codeforces.com/problemset/problem/522/A 题意:给定某条消息的传播路径。问最远传播的距离。。 思路:其实就是问树的深度。。直接dfs就行了。。

codeforces 377 A maze

·2 分钟
http://codeforces.com/contest/377/problem/A 题意:给定一个n*m的maze. ‘.’代表空,‘#’代表墙。要求构造一种方案,使得将k个空格填成墙壁后不影响当前的连通性(即没有被填充的空格之间可以相互到达) 思路:一开始想从上往下从左往右构造。错误的认为四个角一定是可以变成墙的。