結果

問題 No.1502 Many Simple Additions
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-05-08 01:14:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 5,497 bytes
コンパイル時間 4,016 ms
コンパイル使用メモリ 211,124 KB
実行使用メモリ 32,164 KB
最終ジャッジ日時 2023-10-13 22:42:11
合計ジャッジ時間 8,155 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 138 ms
14,452 KB
testcase_28 AC 142 ms
15,188 KB
testcase_29 AC 20 ms
12,292 KB
testcase_30 AC 269 ms
32,156 KB
testcase_31 AC 263 ms
32,088 KB
testcase_32 AC 262 ms
32,164 KB
testcase_33 AC 178 ms
20,576 KB
testcase_34 AC 191 ms
21,576 KB
testcase_35 AC 182 ms
20,860 KB
testcase_36 AC 98 ms
14,752 KB
testcase_37 AC 97 ms
14,940 KB
testcase_38 AC 132 ms
17,360 KB
testcase_39 AC 77 ms
11,672 KB
testcase_40 AC 40 ms
7,768 KB
testcase_41 AC 11 ms
5,984 KB
testcase_42 AC 198 ms
26,840 KB
testcase_43 AC 1 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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
#define fi first
#define se second
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 */
using mint = modint1000000007;
const ll INF = 1000000000000000000ll;
mint 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 mint(0);
          }
        } else {
          ll p=d+bs[v];
          if(p%2){
            return mint(0);
          }
          else{
            queue<ll> S;
            vector<ll> k(n,-1);
            S.push(v);
            k[v]=p/2;
            while(!S.empty()){
              ll f=S.front();
              S.pop();
              for(auto z:to[f]){
                if(k[z.fi]==-1){
                  k[z.fi]=z.se-k[f];
                  S.push(z.fi);
                }
              }
            }
            for(int i=0;i<n;i++){
              if(1<=k[i]&&k[i]<=X){
                for(auto F:to[i]){
                  if(k[F.fi]+k[i]!=F.se) return mint(0);
                }
              }
              else{
                return mint(0);
              }
            }
            return mint(1);
          }
          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 mint(0);
            }
            if(sol == -INF) sol = csol;
          } else {
            return mint(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 mint(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);
  }
  mint 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);
    ans2*=solve(K-1,p[i].size(),h[i].size(),u,v,s);
   // 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).val()<<endl;
}
0