Skip to main content
  1. Posts/

codeforces 277 A. Learning Languages

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/277/problem/A

题意:有n个人,每个人会一定数目的语言(可能为0),一个人学一门语言的代价为1,人和人之间沟通可以通过任意个中间人翻译。问最少的代价使得这n个人可以相互沟通。

思路:建图方式如下:第i个人会语言j,那么连上i和j+n。然后跑一遍dfs,使得1..n这n个点都被访问过。

结果wa4…觉得算法没问题。。看了官方题解。。发现果然有情况没有考虑到。如果所有的人都什么语言都不会的话,那么答案是不能-1的。。因为。。语言和语言之间不能连边。。改了之后A了。。有点开心。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月05日 星期六 20时55分14秒
 4File Name :code/cf/problem/277A.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=2E2+5;
34int n,m;
35vector<int>edge[N];
36bool vis[N];
37
38void dfs( int x)
39{
40  //  cout<<"x:"<<x<<endl;
41    vis[x] = true;
42    for ( int i = 0 ; i < edge[x].size(); i ++)
43    {
44	int v = edge[x][i];
45//	cout<<"v:"<<v<<endl;
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 %d",&n,&m);
59	int p =  0;
60	for ( int i = 1 ; i <= n ; i++)
61	{
62	    int k ;
63	    scanf("%d",&k);
64	    if (k!=0) p = -1;
65	    for ( int j =  0 ; j < k ; j++)
66	    {
67		int x;
68		scanf("%d",&x);
69		edge[i].push_back(x+n);
70		edge[x+n].push_back(i);
71	    }
72	}
73	int cnt = 0 ;
74	ms(vis,false);
75	for ( int i = 1 ; i <= n ; i++)
76	{
77	    if (!vis[i])
78	    {
79		dfs(i);
80		cnt++;
81	    }
82	}
83	cout<<cnt+p<<endl;
84
85  #ifndef ONLINE_JUDGE
86  fclose(stdin);
87  #endif
88    return 0;
89}

Related

codeforces 217A ice skating

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

codeforces 445 B. DZY Loves Chemistry

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

codeforces 115A A. Party

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

codeforces 522 A. Reposts

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