結果
問題 | No.1502 Many Simple Additions |
ユーザー | PCTprobability |
提出日時 | 2021-05-07 22:10:52 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,647 bytes |
コンパイル時間 | 1,944 ms |
コンパイル使用メモリ | 157,436 KB |
実行使用メモリ | 31,632 KB |
最終ジャッジ日時 | 2024-09-15 10:07:41 |
合計ジャッジ時間 | 6,029 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | WA | - |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | WA | - |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 2 ms
5,376 KB |
testcase_27 | AC | 154 ms
14,720 KB |
testcase_28 | AC | 153 ms
15,148 KB |
testcase_29 | AC | 22 ms
12,544 KB |
testcase_30 | AC | 284 ms
31,580 KB |
testcase_31 | AC | 293 ms
31,632 KB |
testcase_32 | AC | 294 ms
31,612 KB |
testcase_33 | AC | 198 ms
20,536 KB |
testcase_34 | AC | 206 ms
21,492 KB |
testcase_35 | AC | 200 ms
20,744 KB |
testcase_36 | WA | - |
testcase_37 | AC | 113 ms
14,812 KB |
testcase_38 | AC | 142 ms
17,552 KB |
testcase_39 | AC | 84 ms
11,592 KB |
testcase_40 | AC | 45 ms
7,936 KB |
testcase_41 | AC | 12 ms
6,016 KB |
testcase_42 | AC | 214 ms
26,960 KB |
testcase_43 | AC | 2 ms
5,376 KB |
ソースコード
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <cassert> #include <cfloat> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; //#include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i) #define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i) #define Sort(v) sort(v.begin(), v.end()) #define rSort(v) sort(v.rbegin(), v.rend()) #define Reverse(v) reverse(v.begin(), v.end()) #define Lower_bound(v, y) \ distance(v.begin(), lower_bound(v.begin(), v.end(), y)) #define Upper_bound(v, y) \ distance(v.begin(), upper_bound(v.begin(), v.end(), y)) #define pb push_back using ll = long long; using ull = unsigned long long; ll mod = 1000000007; using P = pair<ll, ll>; using T = tuple<ll, ll, ll>; using vll = vector<ll>; using vP = vector<P>; using vT = vector<T>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vvP = vector<vector<P>>; using dqll = deque<ll>; ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0}; ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } /* Macros reg. ends here */ const ll INF = 1LL << 50; ll solve(ll X,ll n,ll m,vector<ll> u,vector<ll> v,vector<ll> s) { if(n==1) return X; // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); vvP to(n); ll sol = -INF; rep(i, m) { to[u[i]].emplace_back(v[i], s[i]); to[v[i]].emplace_back(u[i], s[i]); } vll as(n, 0), bs(n, 0); // ax + b queue<ll> q; q.push(0); as[0] = 1, bs[0] = 0; while (!q.empty()) { ll u = q.front(); q.pop(); ll a = as[u], b = bs[u]; for (auto [v, s] : to[u]) { ll c = -a, d = s - b; if (as[v] != 0) { ll cc = as[v], dd = bs[v]; if (cc == c) { if (dd == d) continue; else { return 0; } } else { bool cond = ((d - dd) % 2 == 0) && (((d - dd) / (cc - c)) >= 1); if (cond) { ll csol = (d - dd) / (cc - c); if(sol >= 1 && csol != sol) { return 0; } if(sol == -INF) sol = csol; } else { return 0; } } } else { as[v] = c; bs[v] = d; q.push(v); } } } ll l = 1, r = X; rep(i, n){ ll a = as[i], b = bs[i]; if(a == 1){ chmax(l, 1 - b); chmin(r, X - b); } else { assert(a == -1); chmin(r, b - 1); chmax(l, b - X); } } ll ans = max(0LL, r - l + 1); if(sol >= 1) chmin(ans, 1LL); return ans; } class UnionFind{ public: vector<ll> par; vector<ll> siz; UnionFind(ll sz_):par(sz_),siz(sz_,1ll){ for(int i=0;i<sz_;i++) par[i]=i; } void init(ll sz_){ par.resize(sz_); siz.assign(sz_,1ll); for(int i=0;i<sz_;i++) par[i]=i; } ll root(ll x){ while(par[x]!=x){ x=par[x]=par[par[x]]; } return x; } bool merge(ll x,ll y){ x=root(x); y=root(y); if(x==y) return false; if(siz[x]<siz[y]) swap(x,y); siz[x]+=siz[y]; par[y]=x; return true; } bool issame(ll x,ll y){ return root(x)==root(y); } ll size(ll x){ return siz[root(x)]; } }; int main(){ ll n,m,K; cin>>n>>m>>K; vector<ll> a(m),b(m),c(m); UnionFind T(n); for(int i=0;i<m;i++){ cin>>a[i]>>b[i]>>c[i]; a[i]--; b[i]--; T.merge(a[i],b[i]); } vector<vector<ll>> p(n); for(int i=0;i<n;i++){ p[T.root(i)].push_back(i); } vector<vector<ll>> h(n); for(int i=0;i<m;i++){ h[T.root(a[i])].pb(i); } ll ans=1,ans2=1; for(int i=0;i<n;i++){ vector<ll> u,v,s; map<ll,ll> M; for(int j=0;j<p[i].size();j++){ M[p[i][j]]=j; } for(int j=0;j<h[i].size();j++){ u.pb(M[a[h[i][j]]]); v.pb(M[b[h[i][j]]]); s.pb(c[h[i][j]]); } if(p[i].size()==0) continue; ans*=solve(K,p[i].size(),h[i].size(),u,v,s); ans%=mod; ans2*=solve(K-1,p[i].size(),h[i].size(),u,v,s); ans2%=mod; // cout<<solve(K,p[i].size(),h[i].size(),u,v,s)<<" "<<solve(K-1,p[i].size(),h[i].size(),u,v,s)<<endl; } cout<<((ans-ans2)%mod+mod)%mod<<endl; }