結果

問題 No.1502 Many Simple Additions
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-05-07 22:52:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,510 bytes
コンパイル時間 7,158 ms
コンパイル使用メモリ 316,092 KB
実行使用メモリ 36,364 KB
最終ジャッジ日時 2023-10-13 14:11:03
合計ジャッジ時間 11,693 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 WA -
testcase_19 AC 2 ms
4,348 KB
testcase_20 WA -
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,352 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 20 ms
12,320 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 215 ms
21,760 KB
testcase_34 AC 227 ms
23,372 KB
testcase_35 AC 209 ms
22,128 KB
testcase_36 AC 103 ms
13,904 KB
testcase_37 AC 106 ms
13,916 KB
testcase_38 AC 151 ms
17,592 KB
testcase_39 AC 79 ms
10,828 KB
testcase_40 AC 45 ms
7,940 KB
testcase_41 AC 11 ms
5,724 KB
testcase_42 AC 250 ms
30,548 KB
testcase_43 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 P pair<ll,ll>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define in scanner.read_int()
ll mod = 1000000007;
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;
}
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;
}
0