codevs1506 Messenger _codevs

Source: Internet
Author: User

Topic Description Description

A network of friends, if a knows B, then if a gets a message for the first time, it passes the message to B, and to everyone who knows it.

If a knows b,b not necessarily know a.

All the people from 1 to n numbers, give all "understanding" relationship, ask if I release a new message, then will pass a number of messages, this message back to I,1<=i<=n. Input description Input Description

The first line is N and M, representing the number of people and the number of cognitive relationships.

The next m line, two numbers a and b per line, represents a knowing B. 1<=a, B<=n. The cognitive relationship may be repeated, but a line of two numbers will not be the same.

Output Description Description

Altogether n rows, one character T or F per line. Line I if it is T, I send a new message back to I; if it is F, I send a new message that does not pass back to I.

Sample Input For example

4 6

1 2

2 3

4 1

3 1

1 3

2 3 Sample Output

T

T

T

F range and hint data Size & Hint

n<=1000

1<=a, B<=n


#include <iostream>
#include <memory.h>
#include <set>
using namespace std;
int n;
int m;
Set<int> node[1001];
int visited[1001];
int des;
BOOL Dfs (int i)
{
	if (visited[i] = 1&&i==des) return
		true;
	if (visited[i] = = 1) return
		false;
	Visited[i] = 1;
	For (Set<int>::iterator It=node[i].begin (), It!=node[i].end (); it++)
		if (Dfs (*it) ==true) return
			True ;
	return false;
}


int main ()
{
	cin >> n >> m;
	for (int i=0;i<m;i++)
	{
		int a, b;
		Cin >> a >> b;
		Node[a].insert (b);
	}
	for (int i=1;i<=n;i++)
	{
		memset (visited, 0, sizeof (visited));
		des = i;
		if (Dfs (i))
			cout << "T" <<endl;
		else
			cout << "F" << Endl;
	}
	return 0;
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.