답안 #718639

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
718639 2023-04-04T13:02:21 Z keta_tsimakuridze 통행료 (APIO13_toll) C++17
16 / 100
92 ms 163840 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, h[N], par[N], up[N], SZ[N], sz[N], p[N], P[N];
ll cur = 0;
vector<pair<int,int>> V[N];
void dfs0(int u, int p) {
    h[u] = h[p] + 1;
    SZ[u] = sz[u];
    for(int i = 0; i < V[u].size(); i++) {
        int v = V[u][i].f;
        if(v == p) continue;
        par[v] = u;
        up[v] = (V[u][i].s == 0 ? 0 : 1e6);
        dfs0(v, u);
        SZ[u] += SZ[v];
    }
}
void UP(int u, int v, int w) {
    while(u != v) {
        if(h[u] < h[v]) swap(u, v);
        up[u] = min(up[u], w);
        u = par[u];
    }
}
void dfs(int u, int p) {
    cur += (ll)up[u] * SZ[u];
    for(int i = 0; i < V[u].size(); i++) {
        int v = V[u][i].f;
        if(v == p) continue;
        dfs(v, u);
    }
}
int find(int x){
    return (p[x] == x ? p[x] : p[x] = find(p[x]));
}
int find1(int x) {
    return (P[x] == x ? P[x] : P[x] = find(P[x]));
}
main(){
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector<pair<int, pair<int,int> > > E,ex;
    for(int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        E.push_back({w, {u, v}});
    }
    for(int i = 0; i < k; i++) {
        int u, v; cin >> u >> v;
        E.push_back({0, {u, v}});
    }
    for(int i = 1; i <= n; i++) cin >> sz[i];
    sort(E.begin(), E.end());
    vector<int> X(E.size());
    for(int i = 1; i <= n; i++) p[i] = i;
    for(int i = k; i < E.size(); i++) {
        int u = E[i].s.f, v = E[i].s.s;
        if(find(u) == find(v)) continue;
        X[i] = 1;
        p[find(u)] = find(v);
    }
    for(int i = 1; i <= n; i++) p[i] = i;
    for(int i = 0; i < E.size(); i++) {
        int u = E[i].s.f, v = E[i].s.s;
        if(find(u) == find(v)) {
            if(X[i]) ex.push_back(E[i]);
            X[i] = 0;
            continue;
        }
        X[i] = 1;
        p[find(u)] = find(v);
    }
    for(int i = 1; i <= n; i++) p[i] = i;
    for(int i = k; i < E.size(); i++) {
        int u = E[i].s.f, v = E[i].s.s;
        if(!X[i] || find(u) == find(v)) continue;
        sz[find(v)] += sz[find(u)];
        p[find(u)] = find(v);
    }
    vector<int> x;
    for(int i = 1; i <= n; i++) if(find(i) == i) x.push_back(i);
    ll ans = 0;
    for(int mask = 1; mask < (1 << k); mask++) {
        for(int i = 0; i < x.size(); i++) V[x[i]].clear(), P[x[i]] = x[i];
        int F = 0;
        for(int i = 0; i < k; i++) {
            if((1 << i) & mask) {
                int u = E[i].s.f, v = E[i].s.s;
                u = find(u);
                v = find(v);
                if(find1(u) == find1(v)) F = 1;
                P[find1(u)] = find1(v);
                V[v].push_back({u, 1});
                V[u].push_back({v, 1});
            }
        }
        if(F) continue;
        vector<int> f(ex.size());
        for(int i = 0; i < ex.size(); i++) {
            int u = ex[i].s.f, v = ex[i].s.s;
            u = find(u); v = find(v);
            if(find1(u) != find1(v)) {
                P[find1(u)] = find1(v);
                V[v].push_back({u, 0});
                V[u].push_back({v, 0});
            } else f[i] = 1;
        }
        dfs0(find(1), 0);

        for(int i = 0; i < ex.size(); i++) {
            if(!f[i]) continue;
            int u = ex[i].s.f, v = ex[i].s.s, w = ex[i].f;
            u = find(u); v = find(v);
            UP(u, v, w);
        }
        cur = 0;
        dfs(find(1), 0);
        ans = max(ans, cur);
    } cout << ans;
 }

Compilation message

toll.cpp: In function 'void dfs0(int, int)':
toll.cpp:14:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for(int i = 0; i < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
toll.cpp: In function 'void dfs(int, int)':
toll.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(int i = 0; i < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
toll.cpp: At global scope:
toll.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main(){
      | ^~~~
toll.cpp: In function 'int main()':
toll.cpp:62:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for(int i = k; i < E.size(); i++) {
      |                    ~~^~~~~~~~~~
toll.cpp:69:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for(int i = 0; i < E.size(); i++) {
      |                    ~~^~~~~~~~~~
toll.cpp:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for(int i = k; i < E.size(); i++) {
      |                    ~~^~~~~~~~~~
toll.cpp:90:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for(int i = 0; i < x.size(); i++) V[x[i]].clear(), P[x[i]] = x[i];
      |                        ~~^~~~~~~~~~
toll.cpp:105:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |         for(int i = 0; i < ex.size(); i++) {
      |                        ~~^~~~~~~~~~~
toll.cpp:116:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |         for(int i = 0; i < ex.size(); i++) {
      |                        ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Runtime error 92 ms 163840 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Runtime error 92 ms 163840 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Runtime error 92 ms 163840 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Runtime error 92 ms 163840 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -