Submission #409499

# Submission time Handle Problem Language Result Execution time Memory
409499 2021-05-21T00:08:31 Z rrrr10000 Pipes (CEOI15_pipes) C++14
50 / 100
1982 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
struct UnionFind {
  vector< int > data;
  UnionFind() = default;
  explicit UnionFind(size_t sz) : data(sz, -1) {}
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return false;
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }
  int find(int k) {
    if(data[k] < 0) return (k);
    return data[k] = find(data[k]);
  }
  int size(int k) {
    return -data[find(k)];
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};
struct IncrementalBridgeConnectivity {
private:
  UnionFind cc, bcc;
  vector< int > bbf;
  size_t bridge;

  int size() {
    return bbf.size();
  }

  int par(int x) {
    return bbf[x] == size() ? size() : bcc.find(bbf[x]);
  }

  int lca(int x, int y) {
    unordered_set< int > used;
    for(;;) {
      if(x != size()) {
        if(!used.insert(x).second) return x;
        x = par(x);
      }
      swap(x, y);
    }
  }

  void compress(int x, int y) {
    while(bcc.find(x) != bcc.find(y)) {
      int nxt = par(x);
      bbf[x] = bbf[y];
      bcc.unite(x, y);
      x = nxt;
      --bridge;
    }
  }

  void link(int x, int y) {
    int v = x, pre = y;
    while(v != size()) {
      int nxt = par(v);
      bbf[v] = pre;
      pre = v;
      v = nxt;
    }
  }

public:
  IncrementalBridgeConnectivity() = default;

  explicit IncrementalBridgeConnectivity(int sz) : cc(sz), bcc(sz), bbf(sz, sz), bridge(0) {}

  int find(int k) {
    return bcc.find(k);
  }

  size_t bridge_size() const {
    return bridge;
  }

  bool add_edge(int x, int y) {
    x = bcc.find(x);
    y = bcc.find(y);
    if(cc.find(x) == cc.find(y)) {
      int w = lca(x, y);
      compress(x, w);
      compress(y, w);
      return false;
    } else {
      if(cc.size(x) > cc.size(y)) swap(x, y);
      link(x, y);
      cc.unite(x, y);
      ++bridge;
      return true;
    }
  }
};
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    ll n,m;cin>>n>>m;
    vector<pair<int,int>> tmp;
    IncrementalBridgeConnectivity br(n);
    rep(i,m){
        ll a,b;cin>>a>>b;a--;b--;
        if(br.add_edge(a,b))tmp.pb(a,b);
        assert(tmp.size()<=n);
    }
    for(auto x:tmp){
        if(br.find(x.fi)!=br.find(x.se))cout<<x.fi+1<<' '<<x.se+1<<endl;
    }
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from pipes.cpp:1:
pipes.cpp: In function 'int main()':
pipes.cpp:156:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
  156 |         assert(tmp.size()<=n);
      |                ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 452 KB Output is correct
2 Correct 5 ms 576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 912 KB Output is correct
2 Correct 184 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 1124 KB Output is correct
2 Correct 368 ms 1212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 488 ms 1324 KB Output is correct
2 Correct 416 ms 14864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 594 ms 2196 KB Output is correct
2 Runtime error 555 ms 19988 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 948 ms 2372 KB Output is correct
2 Runtime error 973 ms 35264 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1255 ms 2676 KB Output is correct
2 Runtime error 1246 ms 43840 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1593 ms 2660 KB Output is correct
2 Runtime error 1587 ms 55284 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1979 ms 9168 KB Output is correct
2 Runtime error 1982 ms 65536 KB Memory limit exceeded