結果

問題 No.1502 Many Simple Additions
ユーザー 👑 kmjpkmjp
提出日時 2021-05-07 23:18:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,271 bytes
コンパイル時間 2,244 ms
コンパイル使用メモリ 209,572 KB
実行使用メモリ 18,056 KB
最終ジャッジ日時 2023-10-13 14:37:47
合計ジャッジ時間 5,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,704 KB
testcase_01 AC 3 ms
5,764 KB
testcase_02 WA -
testcase_03 AC 4 ms
5,744 KB
testcase_04 WA -
testcase_05 AC 3 ms
5,716 KB
testcase_06 AC 3 ms
5,700 KB
testcase_07 AC 3 ms
5,708 KB
testcase_08 AC 3 ms
5,868 KB
testcase_09 AC 4 ms
5,748 KB
testcase_10 AC 4 ms
5,748 KB
testcase_11 AC 3 ms
5,848 KB
testcase_12 WA -
testcase_13 AC 3 ms
5,752 KB
testcase_14 AC 3 ms
5,868 KB
testcase_15 AC 3 ms
5,760 KB
testcase_16 AC 3 ms
5,780 KB
testcase_17 AC 3 ms
5,848 KB
testcase_18 WA -
testcase_19 AC 3 ms
5,752 KB
testcase_20 WA -
testcase_21 AC 3 ms
5,724 KB
testcase_22 AC 3 ms
5,728 KB
testcase_23 WA -
testcase_24 AC 3 ms
5,792 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 9 ms
7,432 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 111 ms
12,088 KB
testcase_34 AC 124 ms
12,852 KB
testcase_35 AC 117 ms
12,256 KB
testcase_36 AC 41 ms
7,612 KB
testcase_37 AC 41 ms
7,532 KB
testcase_38 AC 39 ms
8,548 KB
testcase_39 AC 24 ms
7,156 KB
testcase_40 AC 14 ms
7,344 KB
testcase_41 AC 5 ms
6,280 KB
testcase_42 AC 50 ms
10,376 KB
testcase_43 AC 4 ms
5,748 KB
権限があれば一括ダウンロードができます

ソースコード

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