結果

問題 No.1502 Many Simple Additions
ユーザー 👑 kmjpkmjp
提出日時 2021-05-07 23:21:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,222 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 211,688 KB
実行使用メモリ 17,840 KB
最終ジャッジ日時 2023-10-13 22:38:22
合計ジャッジ時間 5,317 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,764 KB
testcase_01 AC 2 ms
5,852 KB
testcase_02 AC 3 ms
5,812 KB
testcase_03 AC 3 ms
5,764 KB
testcase_04 AC 3 ms
5,808 KB
testcase_05 AC 3 ms
5,828 KB
testcase_06 AC 3 ms
5,756 KB
testcase_07 AC 3 ms
5,700 KB
testcase_08 AC 3 ms
5,752 KB
testcase_09 AC 3 ms
5,680 KB
testcase_10 AC 3 ms
5,700 KB
testcase_11 AC 3 ms
5,968 KB
testcase_12 AC 3 ms
5,680 KB
testcase_13 AC 3 ms
5,756 KB
testcase_14 AC 3 ms
5,964 KB
testcase_15 AC 3 ms
5,852 KB
testcase_16 AC 3 ms
5,700 KB
testcase_17 AC 3 ms
5,720 KB
testcase_18 AC 3 ms
5,692 KB
testcase_19 AC 3 ms
5,760 KB
testcase_20 AC 3 ms
5,824 KB
testcase_21 AC 3 ms
5,712 KB
testcase_22 AC 3 ms
5,712 KB
testcase_23 AC 3 ms
5,764 KB
testcase_24 AC 3 ms
5,864 KB
testcase_25 AC 3 ms
5,752 KB
testcase_26 AC 2 ms
5,816 KB
testcase_27 AC 53 ms
10,868 KB
testcase_28 AC 57 ms
11,276 KB
testcase_29 AC 12 ms
7,448 KB
testcase_30 AC 145 ms
17,780 KB
testcase_31 AC 147 ms
17,840 KB
testcase_32 AC 162 ms
17,808 KB
testcase_33 AC 114 ms
12,172 KB
testcase_34 AC 118 ms
13,000 KB
testcase_35 AC 113 ms
12,296 KB
testcase_36 AC 42 ms
7,756 KB
testcase_37 AC 41 ms
7,508 KB
testcase_38 AC 36 ms
8,732 KB
testcase_39 AC 22 ms
7,348 KB
testcase_40 AC 12 ms
7,324 KB
testcase_41 AC 5 ms
6,364 KB
testcase_42 AC 39 ms
10,128 KB
testcase_43 AC 3 ms
5,808 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);
			set<int> S;
			FORR(c,cand) {
				if(num[0][c]+R==K) S.insert(R);
				if(num[1][c]-L==K) S.insert(L);
			}
			
			to[0]=from[0]*(v-S.size())%mo;
			to[1]=(from[0]*S.size()+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