cogs 3008. Circle of Friends

3008. Circle of Friends

★★ Input file: friendscircle.in   Output file: friendscircle.out   a simple comparison of
the time limit: 1 s memory limit: 256 MB

Description [title]

NOI class has n trainees, because of the limited time to spend together, plus a micro-channel between some students, not between some students. Suppose plus micro-channel relationship with each other, i.e. if a plus b microcells, b will add a micro-channel.

Now there is a breaking news emanating from NOI class 1 student, saw the news of each NOI trainees will be forwarded circle of friends, and added his micro-letter friends will see. NOI no students in the class are not forwarded (because of his relationship and is not).

Tell your friends NOI micro-channel relationship between the students in the class, I ask how many students finally see this news.

[Input Format]

The first line of input contains two integers, the first integer n, the number of participants. Numbered from 1 to n participants. Second integer m, m expressed human friends added micro-letter

Next m lines of two integers a, b, separated by a space, the micro-channel represents a friend added between these two numbers of participants.

[Output format]

Output An integer representing the number of students eventually saw the news.

[Sample input]

6 5
1 2
2 3
4 2
3 4
6 5

[Sample output]

4

【prompt】

1<= n <= 1000, 1 <= m <= 10000。

【source】

In this type.

 

 

This is really a silly question

Constant small

Violence dfs ran!

#include<bits/stdc++.h>
#define maxn 1005
using namespace std;
int n,m;
vector<int> v[maxn];
bool vis[maxn];
int ans=0;
void dfs(int p)
{
    if(vis[p])
        return;
    ans++;
    vis[p]=true;
    for(int i=0;i<v[p].size();i++)
        dfs(v[p][i]);    
}
int main()
{
    freopen("friendscircle.in","r",stdin);
    freopen("friendscircle.out","w",stdout); 
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    Dfs (
    }1);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11344156.html