結果

問題 No.2319 Friends+
ユーザー kotatsugame
提出日時 2023-05-26 22:12:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,535 ms / 3,000 ms
コード長 1,149 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 85,224 KB
実行使用メモリ 91,844 KB
最終ジャッジ日時 2024-12-25 07:26:33
合計ジャッジ時間 20,227 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
//#include<map>
#include<unordered_map>
using namespace std;
int N,M;
int P[20000];
int U[2<<17],V[2<<17],deg[20000];
vector<int>G[20000];
//map<int,int>EX[20000];
unordered_map<int,int>EX[20000];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>P[i];
		P[i]--;
	}
	for(int i=0;i<M;i++)
	{
		cin>>U[i]>>V[i];
		U[i]--,V[i]--;
		if(U[i]==V[i])continue;
		deg[U[i]]++;
		deg[V[i]]++;
	}
	for(int i=0;i<M;i++)
	{
		if(U[i]==V[i])continue;
		if(make_pair(deg[U[i]],U[i])>make_pair(deg[V[i]],V[i]))swap(U[i],V[i]);
		G[U[i]].push_back(V[i]);
		++EX[V[i]][P[U[i]]];
	}
	int Q;cin>>Q;
	for(;Q--;)
	{
		int X,Y;cin>>X>>Y;X--,Y--;
		if(P[X]==P[Y])
		{
			cout<<"No\n";
			continue;
		}
		bool fn=false;
		{
			auto it=EX[X].find(P[Y]);
			if(it!=EX[X].end()&&it->second)fn=true;
		}
		if(!fn)
		{
			for(int v:G[X])if(P[v]==P[Y])
			{
				fn=true;
				break;
			}
		}
		if(!fn)cout<<"No\n";
		else
		{
			cout<<"Yes\n";
			for(int v:G[X])
			{
				//if(!--EX[v][P[X]])EX[v].erase(EX[v].find(P[X]));
				--EX[v][P[X]];
				++EX[v][P[Y]];
			}
			P[X]=P[Y];
		}
	}
}
0