답안 #131276

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131276 2019-07-16T23:09:54 Z duality Collapse (JOI18_collapse) C++11
100 / 100
5743 ms 71332 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 ----------
#include "collapse.h"

map<pii,int> M;
int u[100000],v[100000],pos[100000];
vpii queries[100000];
int active[100000],change[100000],seen[100000];
vpii edges;
bool comp(pii a,pii b) {
    return a.second < b.second;
}
int ans[100000];
int parent[100000];
int find(int n) {
    if (parent[n] != n) parent[n] = find(parent[n]);
    return parent[n];
}
vi adjList[100000];
int visited[100000];
int doDFS(int u) {
    int i;
    int c = 0;
    visited[u] = 1;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (!visited[v]) c += 1+doDFS(v);
    }
    return c;
}
vector<int> simulateCollapse(int N,vector<int> T,vector<int> X,vector<int> Y,vector<int> W,vector<int> P) {
    int i,j,k,l;
    int C = T.size(),Q = W.size(),E = 0;
    for (i = 0; i < C; i++) {
        if (X[i] > Y[i]) swap(X[i],Y[i]);
        if (!M.count(mp(X[i],Y[i]))) u[E] = X[i],v[E] = Y[i],M[mp(X[i],Y[i])] = E++;
        pos[i] = M[mp(X[i],Y[i])];
    }
    for (i = 0; i < Q; i++) queries[W[i]].pb(mp(P[i],i));
    int bs = 2*sqrt(C)+EPS;
    for (i = 0; i < C; i += bs) {
        int e = min(i+bs,C);
        vector<pair<pii,int> > qq;
        for (j = i; j < e; j++) {
            change[pos[j]] = 1;
            for (k = 0; k < queries[j].size(); k++) qq.pb(mp(mp(queries[j][k].first,j),queries[j][k].second));
        }
        for (j = 0; j < E; j++) {
            if (active[j] && !change[j]) edges.pb(mp(u[j],v[j]));
        }
        sort(edges.begin(),edges.end(),greater<pii>());
        sort(qq.begin(),qq.end());
        for (j = 0; j < N; j++) parent[j] = j;
        k = 0;
        int c = 0;
        for (j = (int) qq.size()-1; j >= 0; j--) {
            while ((k < edges.size()) && (edges[k].first > qq[j].first.first)) {
                if (find(edges[k].first) != find(edges[k].second)) {
                    parent[find(edges[k].first)] = find(edges[k].second);
                    c++;
                }
                k++;
            }
            vi toClear;
            for (l = qq[j].first.second; l >= i; l--) {
                if (!seen[pos[l]]) {
                    seen[pos[l]] = 1;
                    if ((T[l] == 0) && (X[l] > qq[j].first.first)) {
                        int px = find(X[l]),py = find(Y[l]);
                        adjList[px].pb(py),adjList[py].pb(px);
                        toClear.pb(px),toClear.pb(py);
                    }
                }
            }
            for (l = qq[j].first.second+1; l < e; l++) {
                if (!seen[pos[l]] && active[pos[l]] && (X[l] > qq[j].first.first)) {
                    int px = find(X[l]),py = find(Y[l]);
                    adjList[px].pb(py),adjList[py].pb(px);
                    toClear.pb(px),toClear.pb(py);
                }
            }
            ans[qq[j].second] += N-qq[j].first.first-1-c;
            for (l = 0; l < toClear.size(); l++) {
                if (!visited[toClear[l]]) ans[qq[j].second] -= doDFS(toClear[l]);
            }
            for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
            for (l = qq[j].first.second; l >= i; l--) seen[pos[l]] = 0;
        }
        sort(edges.begin(),edges.end(),comp);
        for (j = 0; j < N; j++) parent[j] = j;
        k = c = 0;
        for (j = 0; j < qq.size(); j++) {
            while ((k < edges.size()) && (edges[k].second <= qq[j].first.first)) {
                if (find(edges[k].first) != find(edges[k].second)) {
                    parent[find(edges[k].first)] = find(edges[k].second);
                    c++;
                }
                k++;
            }
            vi toClear;
            for (l = qq[j].first.second; l >= i; l--) {
                if (!seen[pos[l]]) {
                    seen[pos[l]] = 1;
                    if ((T[l] == 0) && (Y[l] <= qq[j].first.first)) {
                        int px = find(X[l]),py = find(Y[l]);
                        adjList[px].pb(py),adjList[py].pb(px);
                        toClear.pb(px),toClear.pb(py);
                    }
                }
            }
            for (l = qq[j].first.second+1; l < e; l++) {
                if (!seen[pos[l]] && active[pos[l]] && (Y[l] <= qq[j].first.first)) {
                    int px = find(X[l]),py = find(Y[l]);
                    adjList[px].pb(py),adjList[py].pb(px);
                    toClear.pb(px),toClear.pb(py);
                }
            }
            ans[qq[j].second] += qq[j].first.first+1-c;
            for (l = 0; l < toClear.size(); l++) {
                if (!visited[toClear[l]]) ans[qq[j].second] -= doDFS(toClear[l]);
            }
            for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
            for (l = qq[j].first.second; l >= i; l--) seen[pos[l]] = 0;
        }
        for (j = i; j < e; j++) active[pos[j]] ^= 1,change[pos[j]] = 0;
        edges.clear();
    }
    vi ret;
    for (i = 0; i < Q; i++) ret.pb(ans[i]);
    return ret;
}

Compilation message

collapse.cpp: In function 'int doDFS(int)':
collapse.cpp:79:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:100:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (k = 0; k < queries[j].size(); k++) qq.pb(mp(mp(queries[j][k].first,j),queries[j][k].second));
                         ~~^~~~~~~~~~~~~~~~~~~
collapse.cpp:111:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while ((k < edges.size()) && (edges[k].first > qq[j].first.first)) {
                     ~~^~~~~~~~~~~~~~
collapse.cpp:137:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) {
                         ~~^~~~~~~~~~~~~~~~
collapse.cpp:140:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
                         ~~^~~~~~~~~~~~~~~~
collapse.cpp:146:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < qq.size(); j++) {
                     ~~^~~~~~~~~~~
collapse.cpp:147:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while ((k < edges.size()) && (edges[k].second <= qq[j].first.first)) {
                     ~~^~~~~~~~~~~~~~
collapse.cpp:173:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) {
                         ~~^~~~~~~~~~~~~~~~
collapse.cpp:176:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 5496 KB Output is correct
2 Correct 9 ms 5368 KB Output is correct
3 Correct 11 ms 5240 KB Output is correct
4 Correct 11 ms 5368 KB Output is correct
5 Correct 25 ms 5548 KB Output is correct
6 Correct 51 ms 6028 KB Output is correct
7 Correct 9 ms 5492 KB Output is correct
8 Correct 10 ms 5368 KB Output is correct
9 Correct 27 ms 5884 KB Output is correct
10 Correct 43 ms 6008 KB Output is correct
11 Correct 62 ms 6392 KB Output is correct
12 Correct 63 ms 6620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 10216 KB Output is correct
2 Correct 58 ms 9808 KB Output is correct
3 Correct 685 ms 14072 KB Output is correct
4 Correct 84 ms 9700 KB Output is correct
5 Correct 1036 ms 14724 KB Output is correct
6 Correct 386 ms 10744 KB Output is correct
7 Correct 3190 ms 23764 KB Output is correct
8 Correct 1185 ms 18768 KB Output is correct
9 Correct 60 ms 10984 KB Output is correct
10 Correct 74 ms 10408 KB Output is correct
11 Correct 253 ms 10808 KB Output is correct
12 Correct 1338 ms 20500 KB Output is correct
13 Correct 2474 ms 22400 KB Output is correct
14 Correct 3934 ms 26156 KB Output is correct
15 Correct 3185 ms 25980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 10092 KB Output is correct
2 Correct 66 ms 9520 KB Output is correct
3 Correct 84 ms 9796 KB Output is correct
4 Correct 98 ms 9596 KB Output is correct
5 Correct 192 ms 9640 KB Output is correct
6 Correct 471 ms 10520 KB Output is correct
7 Correct 2835 ms 21652 KB Output is correct
8 Correct 4527 ms 46156 KB Output is correct
9 Correct 70 ms 11112 KB Output is correct
10 Correct 284 ms 10588 KB Output is correct
11 Correct 4698 ms 70840 KB Output is correct
12 Correct 5713 ms 46620 KB Output is correct
13 Correct 4874 ms 64420 KB Output is correct
14 Correct 5743 ms 46336 KB Output is correct
15 Correct 4806 ms 71332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 5496 KB Output is correct
2 Correct 9 ms 5368 KB Output is correct
3 Correct 11 ms 5240 KB Output is correct
4 Correct 11 ms 5368 KB Output is correct
5 Correct 25 ms 5548 KB Output is correct
6 Correct 51 ms 6028 KB Output is correct
7 Correct 9 ms 5492 KB Output is correct
8 Correct 10 ms 5368 KB Output is correct
9 Correct 27 ms 5884 KB Output is correct
10 Correct 43 ms 6008 KB Output is correct
11 Correct 62 ms 6392 KB Output is correct
12 Correct 63 ms 6620 KB Output is correct
13 Correct 49 ms 10216 KB Output is correct
14 Correct 58 ms 9808 KB Output is correct
15 Correct 685 ms 14072 KB Output is correct
16 Correct 84 ms 9700 KB Output is correct
17 Correct 1036 ms 14724 KB Output is correct
18 Correct 386 ms 10744 KB Output is correct
19 Correct 3190 ms 23764 KB Output is correct
20 Correct 1185 ms 18768 KB Output is correct
21 Correct 60 ms 10984 KB Output is correct
22 Correct 74 ms 10408 KB Output is correct
23 Correct 253 ms 10808 KB Output is correct
24 Correct 1338 ms 20500 KB Output is correct
25 Correct 2474 ms 22400 KB Output is correct
26 Correct 3934 ms 26156 KB Output is correct
27 Correct 3185 ms 25980 KB Output is correct
28 Correct 44 ms 10092 KB Output is correct
29 Correct 66 ms 9520 KB Output is correct
30 Correct 84 ms 9796 KB Output is correct
31 Correct 98 ms 9596 KB Output is correct
32 Correct 192 ms 9640 KB Output is correct
33 Correct 471 ms 10520 KB Output is correct
34 Correct 2835 ms 21652 KB Output is correct
35 Correct 4527 ms 46156 KB Output is correct
36 Correct 70 ms 11112 KB Output is correct
37 Correct 284 ms 10588 KB Output is correct
38 Correct 4698 ms 70840 KB Output is correct
39 Correct 5713 ms 46620 KB Output is correct
40 Correct 4874 ms 64420 KB Output is correct
41 Correct 5743 ms 46336 KB Output is correct
42 Correct 4806 ms 71332 KB Output is correct
43 Correct 1277 ms 17400 KB Output is correct
44 Correct 3815 ms 25528 KB Output is correct
45 Correct 1671 ms 17964 KB Output is correct
46 Correct 4491 ms 46052 KB Output is correct
47 Correct 68 ms 10984 KB Output is correct
48 Correct 83 ms 10412 KB Output is correct
49 Correct 316 ms 10676 KB Output is correct
50 Correct 1046 ms 12304 KB Output is correct
51 Correct 1865 ms 19608 KB Output is correct
52 Correct 3585 ms 21356 KB Output is correct
53 Correct 3044 ms 39248 KB Output is correct
54 Correct 4272 ms 22720 KB Output is correct
55 Correct 3415 ms 37484 KB Output is correct
56 Correct 3940 ms 45872 KB Output is correct
57 Correct 4530 ms 60740 KB Output is correct
58 Correct 5185 ms 33580 KB Output is correct
59 Correct 4617 ms 57668 KB Output is correct
60 Correct 5667 ms 46536 KB Output is correct