Submission #120840

# Submission time Handle Problem Language Result Execution time Memory
120840 2019-06-25T16:00:14 Z duality Toll (APIO13_toll) C++11
100 / 100
1794 ms 17628 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

struct edge { int a,b,c; };
bool comp(edge a,edge b) {
    return a.c < b.c;
}
edge edges[300000];
int x[20],y[20];
int P[100000];
int ds[100000];
int find(int n) {
    if (ds[n] != n) ds[n] = find(ds[n]);
    return ds[n];
}
vpii adjList[100000];
int com[100000];
int doDFS(int u,int c) {
    if (com[u] != -1) return 0;
    int i;
    com[u] = c;
    for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i].first,c);
    return 0;
}
LLI P2[21];
int yes[21];
int parent[21][5],mm[21][5],depth[21];
LLI size[21];
int doDFS2(int u,int p,int d) {
    int i;
    parent[u][0] = p,mm[u][0] = 1e9,depth[u] = d,size[u] = P2[u];
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i].first;
        if (v != p) doDFS2(v,u,d+1),size[u] += size[v];
    }
    return 0;
}
int main() {
    int i;
    int N,M,K;
    scanf("%d %d %d",&N,&M,&K);
    for (i = 0; i < M; i++) {
        scanf("%d %d %d",&edges[i].a,&edges[i].b,&edges[i].c);
        edges[i].a--,edges[i].b--;
    }
    for (i = 0; i < K; i++) scanf("%d %d",&x[i],&y[i]),x[i]--,y[i]--;
    for (i = 0; i < N; i++) scanf("%d",&P[i]);
    sort(edges,edges+M,comp);

    for (i = 0; i < N; i++) ds[i] = i;
    for (i = 0; i < K; i++) {
        int u = x[i],v = y[i];
        if (find(u) != find(v)) ds[find(u)] = find(v);
    }
    for (i = 0; i < M; i++) {
        int u = edges[i].a,v = edges[i].b;
        if (find(u) != find(v)) {
            ds[find(u)] = find(v);
            adjList[u].pb(mp(v,0));
            adjList[v].pb(mp(u,0));
        }
    }
    int c = 0,e = 0;
    fill(com,com+N,-1);
    for (i = 0; i < N; i++) {
        if (com[i] == -1) doDFS(i,c),c++;
    }
    for (i = 0; i < c; i++) ds[i] = i;
    for (i = 0; i < M; i++) {
        edges[e].a = com[edges[i].a],edges[e].b = com[edges[i].b],edges[e].c = edges[i].c;
        if (find(edges[e].a) != find(edges[e].b)) ds[find(edges[e].a)] = find(edges[e].b),e++;
    }
    for (i = 0; i < K; i++) x[i] = com[x[i]],y[i] = com[y[i]];
    for (i = 0; i < N; i++) P2[com[i]] += P[i],adjList[i].clear();
    N = c,M = e;

    int j,k;
    LLI ans = 0;
    for (i = 1; i < (1 << K); i++) {
        for (j = 0; j < N; j++) ds[j] = j;
        for (j = 0; j < K; j++) {
            if (i & (1 << j)) {
                int u = x[j],v = y[j];
                if (find(u) == find(v)) break;
                else {
                    ds[find(u)] = find(v);
                    adjList[u].pb(mp(v,1));
                    adjList[v].pb(mp(u,1));
                }
            }
        }
        if (j == K) {
            for (j = 0; j < M; j++) {
                int u = edges[j].a,v = edges[j].b;
                if (find(u) != find(v)) {
                    ds[find(u)] = find(v);
                    adjList[u].pb(mp(v,0));
                    adjList[v].pb(mp(u,0));
                    yes[j] = 0;
                }
                else yes[j] = 1;
            }
            doDFS2(0,-1,0);
            for (j = 1; (1 << j) < N; j++) {
                for (k = 0; k < N; k++) {
                    if (parent[k][j-1] != -1) parent[k][j] = parent[parent[k][j-1]][j-1];
                    else parent[k][j] = -1;
                    mm[k][j] = 1e9;
                }
            }
            int logn = j;
            for (j = 0; j < M; j++) {
                int u = edges[j].a,v = edges[j].b;
                if (yes[j]) {
                    if (depth[u] < depth[v]) swap(u,v);
                    for (k = logn-1; k >= 0; k--) {
                        if ((parent[u][k] != -1) && (depth[parent[u][k]] >= depth[v]))
                            mm[u][k] = min(mm[u][k],edges[j].c),u = parent[u][k];
                    }
                    if (u != v) {
                        for (k = logn-1; k >= 0; k--) {
                            if (parent[u][k] != parent[v][k]) {
                                mm[u][k] = min(mm[u][k],edges[j].c),u = parent[u][k];
                                mm[v][k] = min(mm[v][k],edges[j].c),v = parent[v][k];
                            }
                        }
                        mm[u][0] = min(mm[u][0],edges[j].c);
                        mm[v][0] = min(mm[v][0],edges[j].c);
                    }
                }
            }
            for (j = logn-1; j >= 1; j--) {
                for (k = 0; k < N; k++) {
                    if (parent[k][j] != -1) {
                        mm[k][j-1] = min(mm[k][j-1],mm[k][j]);
                        mm[parent[k][j-1]][j-1] = min(mm[parent[k][j-1]][j-1],mm[k][j]);
                    }
                }
            }
            LLI sum = 0;
            for (j = 0; j < N; j++) {
                for (k = 0; k < adjList[j].size(); k++) {
                    int v = adjList[j][k].first;
                    if ((v != parent[j][0]) && adjList[j][k].second) sum += mm[v][0]*size[v];
                }
            }
            ans = max(ans,sum);
        }
        for (j = 0; j < N; j++) adjList[j].clear();
    }
    printf("%lld\n",ans);

    return 0;
}

Compilation message

toll.cpp: In function 'int doDFS(int, int)':
toll.cpp:76:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i].first,c);
                 ~~^~~~~~~~~~~~~~~~~~~
toll.cpp: In function 'int doDFS2(int, int, int)':
toll.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
toll.cpp: In function 'int main()':
toll.cpp:196:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (k = 0; k < adjList[j].size(); k++) {
                             ~~^~~~~~~~~~~~~~~~~~~
toll.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d",&N,&M,&K);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d",&edges[i].a,&edges[i].b,&edges[i].c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:100:62: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < K; i++) scanf("%d %d",&x[i],&y[i]),x[i]--,y[i]--;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
toll.cpp:101:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&P[i]);
                             ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 5 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 5 ms 2688 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 7 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 5 ms 2688 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 7 ms 2816 KB Output is correct
7 Correct 233 ms 11000 KB Output is correct
8 Correct 259 ms 11092 KB Output is correct
9 Correct 241 ms 11000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 5 ms 2688 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 7 ms 2816 KB Output is correct
7 Correct 233 ms 11000 KB Output is correct
8 Correct 259 ms 11092 KB Output is correct
9 Correct 241 ms 11000 KB Output is correct
10 Correct 1289 ms 11060 KB Output is correct
11 Correct 1733 ms 11040 KB Output is correct
12 Correct 1794 ms 17628 KB Output is correct