結果

問題 No.2295 Union Path Query (Medium)
ユーザー fumofumofunifumofumofuni
提出日時 2023-05-05 23:18:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,255 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 223,308 KB
実行使用メモリ 110,080 KB
最終ジャッジ日時 2024-05-02 18:29:11
合計ジャッジ時間 46,763 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,504 KB
testcase_01 AC 4 ms
5,504 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 805 ms
83,712 KB
testcase_06 AC 727 ms
94,336 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 555 ms
110,080 KB
testcase_16 AC 561 ms
110,080 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 813 ms
87,040 KB
testcase_21 AC 832 ms
86,912 KB
testcase_22 WA -
testcase_23 AC 743 ms
71,040 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 752 ms
75,648 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 755 ms
74,624 KB
testcase_32 AC 753 ms
74,752 KB
testcase_33 WA -
testcase_34 AC 718 ms
74,880 KB
testcase_35 AC 724 ms
75,008 KB
testcase_36 AC 721 ms
74,880 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 748 ms
76,928 KB
testcase_40 AC 711 ms
75,136 KB
testcase_41 AC 735 ms
75,904 KB
testcase_42 AC 749 ms
76,160 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 AC 704 ms
75,264 KB
testcase_54 WA -
testcase_55 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}
vl ans(300010,-1);
vector<vpl> meet;
struct UnionFind {
    vector<int> par;
    vector<int> edge;
    vector<ll> fs;
    vector<set<ll>> com;
    
    UnionFind(int n) : par(n, -1),edge(n, 0) {
      fs.resize(n);com.resize(n);
      rep(i,n)com[i].insert(i);
    }

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y,ll w) {
        x = root(x); y = root(y);
        if (x == y) {
            edge[x]++;
            return false;
        }
        if (par[x] > par[y]) {
          swap(x, y);
        }
        ll p=size(x),q=size(y);
        par[x] += par[y];
        par[y] = x;
        edge[x] += edge[y]+1;
        p*=q;p%=MOD9;p*=w;
        fs[x]=(fs[x]+fs[y])%MOD9;
        fs[x]=(fs[x]+p)%MOD9;
        if(com[x].size()<com[y].size())swap(com[x],com[y]);
        for(auto p:com[y]){
          for(auto q:meet[p]){
            if(com[x].count(q.first)){
              ans[q.second]=w;
            }
          }
        }
        for(auto p:com[y])com[x].insert(p);
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }

  ll query(int x) {
    x=root(x);
    return fs[x];
  }
};
int main(){
  ll n,x,q;cin >> n >> x >> q;meet.resize(n);
  UnionFind uf(n);
  vvl qs(q);
  ll idx=0;
  //return 0;
  rep(i,q){
    ll t;cin >> t;qs[i].emplace_back(t);
    if(t==1){
      ll v,w;cin >> v >> w;qs[i].emplace_back(v);qs[i].emplace_back(w);
    }
    else if(t==2){
      ll u,v;cin >> u >> v;
      meet[u].push_back({v,idx});
      meet[v].push_back({u,idx});
      qs[i].emplace_back(idx);idx++;
    }
    else if(t==3){
      ll v;cin >> v;qs[i].emplace_back(v);
    }
    else{
      ll v;cin >> v;qs[i].emplace_back(v);
    }
  }
  //return 0;
  rep(i,q){
    ll t=qs[i][0];
    if(t==1){
      uf.merge(qs[i][1],x,qs[i][2]);
    }
    else if(t==2){
      ll f=ans[qs[i][1]];
      cout << f << endl;
      if(f!=-1)x+=f;x%=n;
    }
    else if(t==3){
      cout << uf.query(qs[i][1]) << endl;
    }
    else{
      ll v=qs[i][1];x+=v;x%=n;
    }
    /*rep(i,n){
      for(auto p:uf.sts[i])cout << p.first <<" " << p.second <<" ";cout << endl;
    }*/
  }
} 
0