結果

問題 No.3426 Mod K Graph Increments (Hard)
コンテスト
ユーザー 沙耶花
提出日時 2026-01-11 15:57:15
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 1,269 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,277 ms
コンパイル使用メモリ 281,832 KB
実行使用メモリ 16,724 KB
最終ジャッジ日時 2026-01-11 15:57:22
合計ジャッジ時間 5,873 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001LL
void dfs(int cv,int pv,vector<vector<int>> &E,vector<int> &pos){
	if(pv != -1){
		pos[cv] = pos[pv] ^ 1;
	}
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to!=pv)dfs(to,cv,E,pos);
	}
	
	
}

int main(){
	
	int _t;
	cin>>_t;
	rep(_,_t){
		int n,m,k;
		cin>>n>>m>>k;
		vector<vector<int>> E(n);
		vector<pair<int,int>> t;
		dsu D(n);
		rep(i,m){
			int u,v;
			cin>>u>>v;
			u--,v--;
			if(D.same(u,v)){
				t.emplace_back(u,v);
			}
			else{
				E[u].push_back(v);
				E[v].push_back(u);
				D.merge(u,v);
			}
		}
		vector<int> b(n);
		rep(i,n)cin>>b[i];
		vector<int> pos(n);
		dfs(0,-1,E,pos);
		long long sum = 0;
		rep(i,n){
			if(pos[i]==0)sum += b[i];
			else sum -= b[i];
		}
		sum %= k;
		if(sum<0)sum += k;
		if(sum==0)cout<<"Yes"<<endl;
		else{
			if(t.size()==0)cout<<"No"<<endl;
			else{
				bool f = false;
				rep(i,t.size()){
					if(pos[t[i].first]==pos[t[i].second])f = true;
				}
				if(k%2==0 && sum%2==1)f = false;
				if(f)cout<<"Yes"<<endl;
				else cout<<"No"<<endl;
			}
		}
		
	}
	return 0;
}
0