반응형

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.

These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.

Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.

Input

The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.

We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Output

Print a single integer — the maximum length of a repost chain.

 

 

<DFS코드>

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

using namespace std;
vector<int > s[201];
map<string, int> dic;

int dp[201];
int n, k, b;
const string joker = "polycarp";

void change(string &reposted)
{
	int len = reposted.length();
	for (int i = 0; i < len; i++)
		if (isupper(reposted[i]))
			reposted[i] = towlower(reposted[i]);
}

void init()
{
	dic.clear();
	for (int i = 0; i < 201; i++)
		s[i].clear();
	k = 0;
}

void dfs(int u, int p)
{
	dp[u] = 1;
	for (int i = 0; i < s[u].size(); i++) {
		int v = s[u][i];
		if (v == p) continue;
		dfs(v, u);
		dp[u] = max(dp[u], dp[v] + 1);
	}
}
int main()
{
	scanf("%d", &n);
	init();
	string name1, name2, reposted;
	int u, v;
	while (n--)
	{
		cin >> name1 >> reposted >> name2;
		change(name1);
		change(name2);

		if (!dic[name1]) dic[name1] = ++k;
		if (!dic[name2]) dic[name2] = ++k;
		s[dic[name1]].push_back(dic[name2]);
		s[dic[name2]].push_back(dic[name1]);
	}
	b = dic[joker];
	dfs(b, -1);
	printf("%d\n", dp[b]);
}

 

<map과 rank를 이용한 코드>

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main() {
	int n;
	int length = 0;
	map<string, int> rank;
	rank["polycarp"] = 1;

	scanf("%d\n", &n);

	while (n--)
	{
		string name1, reposted, name2;
		cin >> name1 >> reposted >> name2;
		// transform은 범위 내 (begin 부터 end 전 까지) 원소들 각각에 대해 인자로 전달한 함수를 실행 한 후, 그 결과를 begin 에서 부터 쭉 기록
		transform(name2.begin(), name2.end(), name2.begin(), ::tolower);
		transform(name1.begin(), name1.end(), name1.begin(), ::tolower);
		rank[name1] = rank[name2] + 1;
		
		if (rank[name1] > length) 
			length = rank[name1]; 
	}

	printf("%d", length);
	return 0;
}

 

 

 

https://codeforces.com/problemset/problem/522/A

 

Problem - 522A - Codeforces

 

codeforces.com

 

반응형

+ Recent posts