結果

問題 No.1502 Many Simple Additions
ユーザー kmjpkmjp
提出日時 2021-05-07 23:18:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,271 bytes
コンパイル時間 2,047 ms
コンパイル使用メモリ 206,080 KB
最終ジャッジ日時 2025-01-21 08:49:02
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 2
other AC * 28 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M,K;
vector<pair<int,int>> E[101010];
const ll mo=1000000007;

ll num[2][101010];
set<int> cand;

void dfs(int cur,int v,int id) {
	cand.insert(cur);
	if(num[id][cur]!=-1LL<<60) {
		if(num[id][cur]!=v) {
			cout<<0<<endl;
			exit(0);
		}
		return;
	}
	num[id][cur]=v;
	FORR2(e,c,E[cur]) dfs(e,c-v,id^1);
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>K;
	K--;
	FOR(i,M) {
		cin>>x>>y>>k;
		k-=2;
		if(k<0) return _P("0\n");
		E[x-1].push_back({y-1,k});
		E[y-1].push_back({x-1,k});
	}
	FOR(i,N) num[0][i]=num[1][i]=-1LL<<60;
	
	ll from[2]={1,0};
	FOR(i,N) if(num[0][i]==-1LL<<60&&num[1][i]==-1LL<<60) {
		ll to[2]={0,0};
		cand.clear();
		dfs(i,0,0);
		
		if(num[1][i]==-1LL<<60) {
			ll L=0,R=K;
			FORR(c,cand) {
				ll a=num[0][c];
				ll b=num[1][c];
				if(a!=-1LL<<60) {
					L=max(L,-a);
					R=min(R,K-a);
				}
				else {
					L=max(L,b-K);
					R=min(R,b);
				}
			}
			if(L>R) {
				cout<<0<<endl;
				return;
			}
			ll v=(R-L+1);
			if(cand.size()==1) {
				to[0]=from[0]*(v-1)%mo;
				to[1]=(from[0]+from[1]*v)%mo;
			}
			else if(v==1) {
				to[0]=0;
				to[1]=(from[0]+from[1])%mo;
			}
			else {
				to[0]=from[0]*(v-2)%mo;
				to[1]=(from[0]*2+from[1]*v)%mo;
			}
		}
		else {
			int ok=1;
			int ma=0;
			FORR(c,cand) {
				ll v=num[0][c]+num[1][c];
				if(v%2||v<0) {
					cout<<0<<endl;
					return;
				}
				v/=2;
				if(v==K) ma=1;
			}
			to[ma]=(from[0]+from[1])%mo;
		}
		swap(from,to);
	}
	cout<<from[1]<<endl;
	
		
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0