답안 #447840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447840 2021-07-27T17:14:40 Z Jasiekstrz Magenta (COCI21_magenta) C++17
110 / 110
245 ms 32428 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5;
vector<pair<int,int>> e[N+10];
vector<pair<int,int>> eok[N+10][2];
pair<int,int> nxt[N+10][2];
map<pair<int,int>,int> dp;
set<pair<int,int>> stck;
void dfs_path(int x,int p,int pc,int t)
{
	nxt[x][t]={p,pc};
	for(auto [v,c]:e[x])
	{
		if(v!=p)
			dfs_path(v,x,c,t);
	}
	return;
}
int solve(int x[2],int t)
{
	if(dp.find({x[0],x[1]})!=dp.end())
		return dp[make_pair(x[0],x[1])];
	if(stck.find({x[0],x[1]})!=stck.end())
		return 2;
	stck.insert({x[0],x[1]});

	int w=1-t;
	auto improve=[&w,&t](int c)
	{
		if(c==t)
			w=c;
		else if(c==2 && w!=t)
			w=c;
		return;
	};

	if(nxt[x[t]][t].se!=1-t && nxt[x[t]][t].fi!=x[1-t])
	{
		int y[2]={x[0],x[1]};
		y[t]=nxt[x[t]][t].fi;
		w=solve(y,1-t);
	}
	else if(nxt[x[t]][t].fi!=x[1-t])
	{
		int ee=-1;
		for(auto [v,c]:eok[x[t]][t])
		{
			if(c!=1-t && v!=x[1-t])
			{
				ee=v;
				break;
			}
		}
		if(ee==-1)
			w=1-t;
		else
		{
			int y[2]={x[0],x[1]};
			y[t]=ee;
			w=solve(y,1-t);
		}
	}
	else
	{
		for(auto [v,c]:eok[x[t]][t])
		{
			if(c!=1-t && v!=x[1-t])
			{
				int y[2]={x[0],x[1]};
				y[t]=v;
				auto old=nxt[x[1-t]][1-t];
				nxt[x[1-t]][1-t]={x[t],nxt[x[t]][t].se};
				improve(solve(y,1-t));
				nxt[x[1-t]][1-t]=old;
			}
		}
	}

	//cerr<<x[0]<<" "<<x[1]<<" "<<t<<": "<<w<<"\n";
	dp[make_pair(x[0],x[1])]=w;
	stck.erase({x[0],x[1]});
	return w;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	int start[2];
	cin>>n;
	cin>>start[0]>>start[1];
	for(int i=1;i<n;i++)
	{
		int a,b;
		string c;
		cin>>a>>b>>c;
		int cc;
		if(c=="plava")
			cc=0;
		else if(c=="crvena")
			cc=1;
		else
			cc=2;
		e[a].emplace_back(b,cc);
		e[b].emplace_back(a,cc);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j:{0,1})
		{
			for(auto [v,c]:e[i])
			{
				if(c!=1-j)
					eok[i][j].emplace_back(v,c);
			}
		}
	}
	dfs_path(start[0],0,0,1);
	dfs_path(start[1],0,1,0);

	int ans=solve(start,0);
	if(ans==0)
		cout<<"Paula\n";
	else if(ans==1)
		cout<<"Marin\n";
	else
		cout<<"Magenta\n";
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7296 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7296 KB Output is correct
4 Correct 4 ms 7244 KB Output is correct
5 Correct 4 ms 7244 KB Output is correct
6 Correct 4 ms 7244 KB Output is correct
7 Correct 4 ms 7244 KB Output is correct
8 Correct 4 ms 7244 KB Output is correct
9 Correct 4 ms 7368 KB Output is correct
10 Correct 5 ms 7372 KB Output is correct
11 Correct 5 ms 7372 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 32428 KB Output is correct
2 Correct 129 ms 24132 KB Output is correct
3 Correct 227 ms 31120 KB Output is correct
4 Correct 98 ms 21316 KB Output is correct
5 Correct 109 ms 21812 KB Output is correct
6 Correct 4 ms 7244 KB Output is correct
7 Correct 5 ms 7244 KB Output is correct
8 Correct 4 ms 7244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7296 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7296 KB Output is correct
4 Correct 4 ms 7244 KB Output is correct
5 Correct 4 ms 7244 KB Output is correct
6 Correct 4 ms 7244 KB Output is correct
7 Correct 4 ms 7244 KB Output is correct
8 Correct 4 ms 7244 KB Output is correct
9 Correct 4 ms 7368 KB Output is correct
10 Correct 5 ms 7372 KB Output is correct
11 Correct 5 ms 7372 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7372 KB Output is correct
14 Correct 245 ms 32428 KB Output is correct
15 Correct 129 ms 24132 KB Output is correct
16 Correct 227 ms 31120 KB Output is correct
17 Correct 98 ms 21316 KB Output is correct
18 Correct 109 ms 21812 KB Output is correct
19 Correct 4 ms 7244 KB Output is correct
20 Correct 5 ms 7244 KB Output is correct
21 Correct 4 ms 7244 KB Output is correct
22 Correct 4 ms 7244 KB Output is correct
23 Correct 4 ms 7372 KB Output is correct
24 Correct 4 ms 7244 KB Output is correct
25 Correct 4 ms 7244 KB Output is correct
26 Correct 5 ms 7244 KB Output is correct
27 Correct 5 ms 7244 KB Output is correct
28 Correct 4 ms 7244 KB Output is correct
29 Correct 4 ms 7368 KB Output is correct
30 Correct 4 ms 7368 KB Output is correct
31 Correct 86 ms 19988 KB Output is correct
32 Correct 86 ms 19892 KB Output is correct
33 Correct 88 ms 19908 KB Output is correct
34 Correct 84 ms 19908 KB Output is correct