結果

問題 No.1502 Many Simple Additions
ユーザー chocoruskchocorusk
提出日時 2021-05-09 13:36:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 2,117 bytes
コンパイル時間 4,031 ms
コンパイル使用メモリ 193,340 KB
実行使用メモリ 12,564 KB
最終ジャッジ日時 2023-10-18 23:57:55
合計ジャッジ時間 8,446 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,400 KB
testcase_01 AC 3 ms
6,400 KB
testcase_02 AC 3 ms
6,400 KB
testcase_03 AC 3 ms
6,400 KB
testcase_04 AC 3 ms
6,400 KB
testcase_05 AC 3 ms
6,400 KB
testcase_06 AC 3 ms
6,400 KB
testcase_07 AC 3 ms
6,400 KB
testcase_08 AC 3 ms
6,400 KB
testcase_09 AC 3 ms
6,400 KB
testcase_10 AC 3 ms
6,404 KB
testcase_11 AC 3 ms
6,404 KB
testcase_12 AC 3 ms
6,404 KB
testcase_13 AC 3 ms
6,400 KB
testcase_14 AC 3 ms
6,400 KB
testcase_15 AC 3 ms
6,400 KB
testcase_16 AC 3 ms
6,404 KB
testcase_17 AC 3 ms
6,404 KB
testcase_18 AC 3 ms
6,408 KB
testcase_19 AC 3 ms
6,404 KB
testcase_20 AC 3 ms
6,412 KB
testcase_21 AC 3 ms
6,404 KB
testcase_22 AC 3 ms
6,404 KB
testcase_23 AC 3 ms
6,404 KB
testcase_24 AC 3 ms
6,404 KB
testcase_25 AC 3 ms
6,404 KB
testcase_26 AC 3 ms
6,408 KB
testcase_27 AC 111 ms
10,612 KB
testcase_28 AC 111 ms
10,808 KB
testcase_29 AC 4 ms
6,460 KB
testcase_30 AC 151 ms
12,564 KB
testcase_31 AC 146 ms
12,564 KB
testcase_32 AC 147 ms
12,560 KB
testcase_33 AC 123 ms
10,664 KB
testcase_34 AC 127 ms
10,904 KB
testcase_35 AC 124 ms
10,724 KB
testcase_36 AC 90 ms
8,288 KB
testcase_37 AC 89 ms
8,288 KB
testcase_38 AC 101 ms
9,812 KB
testcase_39 AC 65 ms
7,972 KB
testcase_40 AC 28 ms
7,668 KB
testcase_41 AC 6 ms
6,748 KB
testcase_42 AC 117 ms
11,336 KB
testcase_43 AC 3 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
int n, m;
ll k;
ll v[100010];
int p[100010];
using mint=modint1000000007;
vector<P> g[100010];
int main()
{
	cin>>n>>m>>k;
	for(int i=0; i<m; i++){
		int x, y, z;
		cin>>x>>y>>z;
		x--; y--;
		g[x].push_back({y, z});
		g[y].push_back({x, z});
	}
	mint c1=1, c2=1;
	bool dame=0;
	vector<int> w;
	ll val=-1;
	auto dfs=[&](auto dfs, int x)->void{
		w.push_back(x);
		for(auto q:g[x]){
			int y=q.first;
			if(p[y]==0){
				p[y]=-p[x];
				v[y]=q.second-v[x];
				dfs(dfs, y);
			}else if(p[y]!=p[x]){
				if(v[y]!=q.second-v[x]){
					dame=1;
				}
			}else{
				ll v1=(q.second-v[x]-v[y])*p[x];
				if(v1%2!=0 || v1<=0 || (val!=-1 && val!=v1/2)){
					dame=1;
				}
				val=v1/2;
			}
		}
	};
	for(int i=0; i<n; i++){
		if(p[i]!=0) continue;
		p[i]=1;
		w.clear();
		val=-1;
		dfs(dfs, i);
		if(dame){
			cout<<0<<endl;
			return 0;
		}
		if(val==-1){
			ll l=1, r=k;
			for(auto x:w){
				if(p[x]==1){
					l=max(l, 1-v[x]);
					r=min(r, k-v[x]);
				}else{
					l=max(l, v[x]-k);
					r=min(r, v[x]-1);
				}
			}
			c1*=mint(max(0ll, r-l+1));
			l=1, r=k-1;
			for(auto x:w){
				if(p[x]==1){
					l=max(l, 1-v[x]);
					r=min(r, k-1-v[x]);
				}else{
					l=max(l, v[x]-(k-1));
					r=min(r, v[x]-1);
				}
			}
			c2*=mint(max(0ll, r-l+1));
		}else{
			for(auto x:w){
				if(p[x]==1) v[x]+=val;
				else v[x]-=val;
				if(v[x]<=0){
					c1=0, c2=0;
				}else if(v[x]==k){
					c2=0;
				}else if(v[x]>k){
					c1=0, c2=0;
				}
			}
		}
	}
	if(dame) cout<<0<<endl;
	else cout<<(c1-c2).val()<<endl;
    return 0;
}
0