본문 바로가기

USACO/Bronze

백준 5938 Daisy Chains in the Field (USACO November 2010 Bronze 1번)

문제 링크: https://www.acmicpc.net/problem/5938

 

5938번: Daisy Chains in the Field

Farmer John let his N (1 <= N <= 250) cows conveniently numbered 1..N play in the field. The cows decided to connect with each other using cow-ropes, creating M (1 <= M <= N*(N-1)/2) pairwise connections. Of course, no two cows had more than one rope direc

www.acmicpc.net

1번 소를 시작으로 dfs를 실행시켜서, 1번 소와 연결되지 않은 소들이 몇개인지 (visit 하지 않은 정점이 몇개인지) 세면 됨.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
 
int n, m, v[251], cnt;
vector<int> adj[251];
 
void dfs(int cur) {
    v[cur] = 1;
    cnt++;
    for(int i = 0; i < adj[cur].size(); i++) {
        int nxt = adj[cur][i];
        if(!v[nxt]) dfs(nxt);
    }
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> n >> m;
    while(m--) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
 
    dfs(1);
 
    if(cnt == n) cout << 0 << '\n';
    for(int i = 1; i <= n; i++){
        if(!v[i])
            cout << i << '\n';
    }
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter