답안 #246469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246469 2020-07-09T11:37:29 Z dantoh000 Pipes (CEOI15_pipes) C++14
100 / 100
1687 ms 13560 KB
#include <utility>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
using namespace std;
typedef pair<int,int> ii;
int n,m;
int d[100005], pa[100005], sz[100005];
vector<int> G[100005];
set<ii> bad;
struct ufds{
    int p[100005];
    ufds(){
        for (int i = 0; i <= 100000; i++) p[i] = i;
    }
    inline int find(int x){
        return p[x] == x ? x : p[x] = find(p[x]);
    }
    inline void un(int x, int y){
        p[x] = y;
    }
} u1 = ufds(), u2 = ufds();
void dfs(int u, int p){
    ///join at side bad
    u2.p[u] = u;
    pa[u] = p;
	for (auto v : G[u]){
		if (v == p) continue;
		else{
            d[v] = d[u]+1;
            dfs(v,u);
		}
	}
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++) sz[i] = 1; ///serious error
    int u,v;
    for (int i = 0; i < m; i++){
        scanf("%d%d",&u,&v);
        int pu = u1.find(u), pv = u1.find(v);
        //printf("%d -> %d, %d -> %d\n",u,pu,v,pv);
        if (pu == pv){
            //printf("update path %d %d\n",u,v);
            ///make all edges along this path bad
            ///all edges along path are only changed once, amortised O(M)
            u = u2.find(u), v = u2.find(v);
            while (u != v){
                if (d[u] < d[v]) swap(u,v);
                bad.insert({min(u,pa[u]),max(u,pa[u])});
                //printf("bad %d %d\n",u,pa[u]);
              	u2.un(u,pa[u]);
                u = u2.find(u);
            }
        }
        else{

            //printf("join %d %d\n",u,v);
            ///join the two components
            if (sz[pu] < sz[pv]){
                swap(pu,pv);
                swap(u,v);
            }
            ///join small v to big u
            u1.un(pv,pu);
            sz[pu] += sz[pv];
            ///add edge
            G[u].push_back(v);
            G[v].push_back(u);
            ///restart dfs
            d[v] = d[u]+1;
            dfs(v,u);

        }
    }
    for (int u = 1; u <= n ;u++){
        for (auto v : G[u]){
            if (v > u && bad.find({u,v}) == bad.end()){
                printf("%d %d\n",u,v);
            }
        }
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
     ~~~~~^~~~~~~~~~~~~~
pipes.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&u,&v);
         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3968 KB Output is correct
2 Correct 16 ms 3968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 5480 KB Output is correct
2 Correct 151 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 6904 KB Output is correct
2 Correct 289 ms 6816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 421 ms 7928 KB Output is correct
2 Correct 352 ms 8416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 586 ms 10592 KB Output is correct
2 Correct 555 ms 9848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 904 ms 12152 KB Output is correct
2 Correct 816 ms 11256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1160 ms 13560 KB Output is correct
2 Correct 1164 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1406 ms 13432 KB Output is correct
2 Correct 1603 ms 13048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1687 ms 13176 KB Output is correct
2 Correct 1643 ms 12668 KB Output is correct