답안 #130395

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
130395 2019-07-15T06:25:28 Z mirbek01 Simurgh (IOI17_simurgh) C++11
0 / 100
4 ms 1916 KB
#include "simurgh.h"
#include <bits/stdc++.h>

using namespace std;

const int N = 3e4 + 2;

int p[N], sz[N], used[N];
vector <int> g[N];

int f_s(int v){
    return (p[v] == v)?v:p[v] = f_s(p[v]);
}

void u_s(int a, int b){
    a = f_s(a);
    b = f_s(b);
    if(a != b){
        if(sz[a] < sz[b])
            swap(a, b);
        p[b] = a;
        sz[a] += sz[b];
    }
}

int tin[N], tout[N], timer;

void dfs(int v, int pr){
    tin[v] = ++ timer;
    for(int to : g[v]){
        if(to == pr)
            continue;
        dfs(to, v);
    }
    tout[v] = timer;
}

bool upper(int a, int b){
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
	vector<int> r(n - 1);
    int m = (int)u.size();
    for(int i = 0; i < n; i ++)
        p[i] = i, sz[i] = 1;
    int pt = 0;
    for(int i = 0; i < m; i ++){
        if(f_s(u[i]) != f_s(v[i])){
            r[pt ++] = i;
            u_s(u[i], v[i]);
            g[u[i]].push_back(v[i]);
            g[v[i]].push_back(u[i]);
        }
    }

    dfs(0, 0);
    int cn = count_common_roads(r);

    for(int i = 0; i < pt; i ++){
        int a = u[ r[i] ], b = v[ r[i] ];
        if(upper(b, a))
            swap(a, b);
        int mx =  0;
        vector <int> golds;
        for(int j = 0; j < m; j ++){
            if(used[j])
                continue;
            int cnt = 0;
            if(upper(b, v[j]))
                cnt ++;
            if(upper(b, u[j]))
                cnt ++;
            if(cnt != 1)
                continue;
            int pre = r[i];
            r[i] = j;
            int kol = count_common_roads(r);
            if(mx < kol){
                while(!golds.empty()){
                    int last = golds.back();
                    used[last] = 1;
                    golds.pop_back();
                }
                mx = kol;
            }
            if(mx == kol){
                golds.emplace_back(j);
            } else {
                used[j] = 1;
            }
        }
        for(int j = 0; j < golds.size(); j ++)
            used[ golds[j] ] = 2;
    }

    pt = 0;

    for(int i = 0; i < m; i ++){
        if(used[i] == 2)
            r[pt ++] = i;
    }

    return r;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:76:17: warning: unused variable 'pre' [-Wunused-variable]
             int pre = r[i];
                 ^~~
simurgh.cpp:93:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < golds.size(); j ++)
                        ~~^~~~~~~~~~~~~~
simurgh.cpp:58:9: warning: unused variable 'cn' [-Wunused-variable]
     int cn = count_common_roads(r);
         ^~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 1912 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 1912 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 1912 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1016 KB correct
2 Runtime error 4 ms 1916 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 1912 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -