結果
問題 | No.1502 Many Simple Additions |
ユーザー | PCTprobability |
提出日時 | 2021-05-07 22:58:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,427 bytes |
コンパイル時間 | 7,404 ms |
コンパイル使用メモリ | 326,200 KB |
実行使用メモリ | 36,528 KB |
最終ジャッジ日時 | 2024-09-15 11:04:15 |
合計ジャッジ時間 | 11,748 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 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 | WA | - |
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 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 22 ms
12,544 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 216 ms
22,724 KB |
testcase_34 | AC | 229 ms
23,964 KB |
testcase_35 | AC | 217 ms
22,960 KB |
testcase_36 | WA | - |
testcase_37 | AC | 114 ms
14,992 KB |
testcase_38 | AC | 151 ms
18,680 KB |
testcase_39 | AC | 86 ms
11,660 KB |
testcase_40 | AC | 47 ms
8,192 KB |
testcase_41 | AC | 12 ms
6,144 KB |
testcase_42 | AC | 243 ms
30,552 KB |
testcase_43 | AC | 2 ms
5,376 KB |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <unistd.h> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using ll = long long; #define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i)) //#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(x) begin(x), end(x) #define all(s) (s).begin(),(s).end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) #define rever(vec) reverse(vec.begin(), vec.end()) #define sor(vec) sort(vec.begin(), vec.end()) #define fi first #define se second #define pb push_back #define REP(i, n) for (int i = 0; i < (n); ++i) #define in scanner.read_int() ll mod = 1000000007; using P = pair<ll, ll>; using T = tuple<ll, ll, ll>; using vll = vector<ll>; const ll INF = 1LL << 50; 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>; 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; } ll solve(ll XT,ll n,ll m,vector<ll> u,vector<ll> v,vector<ll> s){ if(n==1) return XT; vector<vector<int>> E(n); vector<vector<long long>> S(n); for (int i=0; i<m; i++) { E[u[i]].push_back(v[i]); S[u[i]].push_back(s[i]); E[v[i]].push_back(u[i]); S[v[i]].push_back(s[i]); } vector<long long> V(n); vector<bool> X(n); vector<bool> F(n); int cv = -1; long long ca, cb; function<void(int,int,long long,bool)> BT = [&](int c, int p, long long v, bool x) { if (F[c] && X[c] != x) { cv = c; ca = V[c]; cb = v; } if (F[c]) return; F[c] = true; V[c] = v; X[c] = x; for (int i=0; i<(int)E[c].size(); i++) if (E[c][i] != p) BT(E[c][i], c, S[c][i]-v, !x); }; BT(0, -1, 0LL, false); if (cv != -1) { if ((ca-cb)%2 != 0) { return 0; } F = vector<bool>(n); BT(cv, -1, (ca+cb)/2, false); } bool ok = true; bool bi = true; for (int i=0; i<n; i++) { for (int j=0; j<(int)E[i].size(); j++) { if (V[i]+V[E[i][j]] != S[i][j]) ok = false; if (X[i] == X[E[i][j]]) bi = false; } } if (!ok) { return 0; } if (!bi) { if (*min_element(V.begin(), V.end()) > 0&&*max_element(V.begin(), V.end()) <= XT) { return 1; } else{ return 0; } } long long mn0 = XT; long long mn1 = 1; for (int i=0; i<n; i++) { if (X[i]) { mn0 = min(mn0, V[i] - 1); mn1 = max(mn1,- XT + V[i]); } else { mn1 = max(mn1,- V[i] - 1); mn0 = min(mn0, XT - V[i]); } // cout<<mn0<<" "<<mn1<<" "<<X[i]<<" "<<V[i]<<endl; } long long ans = max(mn0-mn1+1, 0LL); // cout<<ans<<endl; return ans; } ll solve2(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*=solve2(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; }