結果

問題 No.2148 ひとりUNO
ユーザー daiotadaiota
提出日時 2022-12-05 05:44:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,314 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 180,852 KB
実行使用メモリ 7,900 KB
最終ジャッジ日時 2024-04-20 08:56:05
合計ジャッジ時間 3,418 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 17 ms
7,664 KB
testcase_17 AC 17 ms
7,788 KB
testcase_18 AC 18 ms
6,940 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 17 ms
7,536 KB
testcase_22 AC 17 ms
6,944 KB
testcase_23 AC 17 ms
6,944 KB
testcase_24 AC 17 ms
6,940 KB
testcase_25 AC 17 ms
6,944 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<char,ll> P;
typedef pair<ll,char> PP;
typedef pair<P,ll> Q;
typedef pair<PP,ll> QQ;

ll par[200010],rk[200010],sz[200010];


void init(ll n){

	REP(i,n+1){
		par[i]=i;
		rk[i]=0;
		sz[i]=1;
	}
}

ll find(ll x){

	if(par[x]==x){
	   return x;
	}else{
	  return par[x]=find(par[x]);
	}
}

void unite(ll x,ll y){

	x=find(x); y=find(y);
	if(x==y) return;

	if(rk[x]<rk[y]){
		par[x]=y;
		sz[y]=sz[x]+sz[y];
	}else{
		par[y]=x;
		sz[x]=sz[x]+sz[y];
		if(rk[x]==rk[y]) rk[x]++;
	}
}

bool same(ll x,ll y){

	return find(x)==find(y);
}

ll size(ll x){

	return sz[find(x)];
}


int main(void){
	ll i;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	int T;
	cin >> T;
	REP(tc,T){

		ll N;
		cin >> N;
		vector<Q> q;
		vector<QQ> qq;
		REP(i,N){
			char c;
			ll x;
			cin >> c >> x;
			q.push_back(Q(P(c,x),i+1));
			qq.push_back(QQ(PP(x,c),i+1));
		}

		sort(q.begin(),q.end());
		sort(qq.begin(),qq.end());

		init(N);

		REP(i,N-1) if(q[i].first.first==q[i+1].first.first) unite(q[i].second,q[i+1].second);
		REP(i,N-1) if(qq[i].first.first==qq[i+1].first.first) unite(qq[i].second,qq[i+1].second);

		if(size(1)==N) cout << "YES" << endl;
		else cout << "NO" << endl;

	}



	return 0;
}
0