Submission #245768

# Submission time Handle Problem Language Result Execution time Memory
245768 2020-07-07T11:04:31 Z icypiggy Bridges (APIO19_bridges) C++14
100 / 100
1524 ms 15600 KB
#include <bits/stdc++.h>
 
using namespace std;
int n,m,qc;
const int n_max = 5e4+10;
const int m_max = 1e5+10;
const int q_max = 1e5+10;
struct edge {
    int u,v,w,s,e;
    edge() : u(0), v(0), w(0), s(0), e(0) {}
    edge(int _u, int _v, int _w, int _s, int _e) : u(_u), v(_v), w(_w), s(_s), e(_e) {}
    bool operator < (edge other) const {
        return w > other.w; // invert comparator
    }
};
struct query {
    int time, start, weight, ans;
    query() : time(0), start(0), weight(0), ans(0) {}
    query(int _t, int _s, int _w) : time(_t), start(_s), weight(_w), ans(0) {}
    bool operator < (query other) const {
        return weight > other.weight;
    }
};
vector<query> q;
vector<edge> init[m_max];
vector<edge> sorted_edge;
int visited[n_max];
int parent[n_max];
int sz[n_max];
// reset the ufds
void reset_ufds() {
    for(int i=0; i<n_max; i++) {
        parent[i] = i;
        sz[i] = 1;
    }
}
int findset(int x) {
    while(parent[x]!=x) {
        x = parent[x] = parent[parent[x]];
    }
    return x;
}
void onion(int x, int y) {
    while(parent[x]!=x) {
        x = parent[x] = parent[parent[x]];
    }
    while(parent[y]!=y) {
        y = parent[y] = parent[parent[y]];
    }
    if(x!=y) {
        if(sz[x]>sz[y]) swap(x,y);
        sz[y] += sz[x];
        parent[x] = y;
    }
}
vector<int> adj[n_max];
vector<pair<int,int>> output;
void ans_query(int first, int last) { // [first,last)
    for(int i=0; i<n_max; i++) {
        adj[i].clear();
    }
    if(first>=last) return;
    reset_ufds();
    int ts = q[first].time;
    int te = q[last-1].time;
    sort(q.begin()+first, q.begin()+last); // sort by weight
    auto it = sorted_edge.begin();
    vector<edge> templist;
    for(int i=first; i<last; i++) {
        //cout << "i: " << i << "\n";
        //cout << q[i].time << " " << q[i].weight << "\n";
        while(it!=sorted_edge.end() && it->w >= q[i].weight) {
            //it->print();
            //cout << "weight: " << it->w << " " << it->s << " " << it->e << "\n";
            if(it->s < ts && it->e > te) {
                onion(it->u, it->v);
            } else if((it->s < te && it->s > ts) || (it->e < te && it->e > ts)) {
                // add it to the list of edges to process
                //cout << "templist: " << it->u << " " << it->v << "\n";
                templist.push_back(*it);
            }
            it++;
        }
        int starting = findset(q[i].start);
        int ans = sz[starting];
        for(edge &e: templist) {
            e.u = findset(e.u);
            e.v = findset(e.v);
            adj[e.u].clear();
            adj[e.v].clear();
        }
        for(edge &e: templist) {
            if(e.s < q[i].time && e.e>q[i].time) {
                adj[e.u].push_back(e.v);
                adj[e.v].push_back(e.u);
            }
        }
        visited[starting] = i; // record the visited time
        //cout << "starting: " << starting << "\n";
        queue<int> bfs;
        bfs.push(starting);
        while(!bfs.empty()) {
            int next = bfs.front();
            //cout << "bfs: " << next << "\n";
            bfs.pop();
            for(int z: adj[next]) {
                z = findset(z);
                if(visited[z]!=i) {
                    visited[z] = i;
                    ans += sz[z];
                    bfs.push(z);
                }
            }
        }
        output.push_back(make_pair(q[i].time, ans));
        //cout << "output: " << q[i].time << " " << ans << "\n";
    }
}
int main() {
    //freopen("../q2/05","r",stdin);
    //freopen("05b","w",stdout);
    cin>>n>>m;
    for(int i=0; i<m; i++) {
        edge e;
        cin >> e.u >> e.v >> e.w;
        e.s = -1;
        e.e = 2e5;
        init[i].push_back(e);
    }
    cin >> qc;
    const int bucket_size = 500;
    vector<int> breakpoints;
    for(int i=0; i<qc; i++) {
        if(i%bucket_size==0) breakpoints.push_back(q.size());
        int a,b,c; cin>>a>>b>>c;
        if(a==2) {
            q.push_back(query(i, b, c));
        } else {
            assert(a==1);
            init[b-1].back().e = i;
            init[b-1].push_back(edge(init[b-1][0].u, init[b-1][0].v, c, i, 2e5));
        }
    }
    for(int i=0; i<m; i++) {
        for(edge e: init[i]) {
            sorted_edge.push_back(e);
        }
    }
    breakpoints.push_back(q.size());
    sort(sorted_edge.begin(), sorted_edge.end());
 
    memset(visited, -1, sizeof(visited));
    for(int i=1; i<breakpoints.size(); i++) {
        //cout << "?\n";
        ans_query(breakpoints[i-1], breakpoints[i]);
    }
    sort(output.begin(), output.end());
    for(int i=1; i<output.size(); i++) {
        assert(output[i].first != output[i-1].first);
    }
    for(auto p: output) {
        cout << p.second << "\n";
    }
    return 0;
}

Compilation message

bridges.cpp: In function 'int main()':
bridges.cpp:153:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<breakpoints.size(); i++) {
                  ~^~~~~~~~~~~~~~~~~~~
bridges.cpp:158:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<output.size(); i++) {
                  ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4480 KB Output is correct
2 Correct 7 ms 4352 KB Output is correct
3 Correct 43 ms 4992 KB Output is correct
4 Correct 22 ms 4860 KB Output is correct
5 Correct 37 ms 4984 KB Output is correct
6 Correct 34 ms 4992 KB Output is correct
7 Correct 36 ms 4988 KB Output is correct
8 Correct 34 ms 4984 KB Output is correct
9 Correct 45 ms 4984 KB Output is correct
10 Correct 34 ms 4984 KB Output is correct
11 Correct 36 ms 4984 KB Output is correct
12 Correct 37 ms 4992 KB Output is correct
13 Correct 39 ms 4980 KB Output is correct
14 Correct 38 ms 4980 KB Output is correct
15 Correct 36 ms 4992 KB Output is correct
16 Correct 37 ms 4984 KB Output is correct
17 Correct 35 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 957 ms 12008 KB Output is correct
2 Correct 964 ms 11996 KB Output is correct
3 Correct 984 ms 12024 KB Output is correct
4 Correct 944 ms 11976 KB Output is correct
5 Correct 940 ms 12144 KB Output is correct
6 Correct 1396 ms 11748 KB Output is correct
7 Correct 1289 ms 11696 KB Output is correct
8 Correct 1247 ms 11492 KB Output is correct
9 Correct 159 ms 8036 KB Output is correct
10 Correct 1067 ms 11876 KB Output is correct
11 Correct 968 ms 11852 KB Output is correct
12 Correct 671 ms 11408 KB Output is correct
13 Correct 956 ms 13724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 819 ms 10872 KB Output is correct
2 Correct 725 ms 9452 KB Output is correct
3 Correct 989 ms 11044 KB Output is correct
4 Correct 834 ms 11108 KB Output is correct
5 Correct 166 ms 8036 KB Output is correct
6 Correct 872 ms 10844 KB Output is correct
7 Correct 746 ms 10908 KB Output is correct
8 Correct 731 ms 10876 KB Output is correct
9 Correct 513 ms 10284 KB Output is correct
10 Correct 479 ms 10248 KB Output is correct
11 Correct 729 ms 11744 KB Output is correct
12 Correct 643 ms 11876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 681 ms 13412 KB Output is correct
2 Correct 158 ms 8036 KB Output is correct
3 Correct 171 ms 9960 KB Output is correct
4 Correct 168 ms 9960 KB Output is correct
5 Correct 608 ms 13664 KB Output is correct
6 Correct 683 ms 13536 KB Output is correct
7 Correct 600 ms 13444 KB Output is correct
8 Correct 541 ms 10620 KB Output is correct
9 Correct 530 ms 10676 KB Output is correct
10 Correct 516 ms 10856 KB Output is correct
11 Correct 683 ms 12008 KB Output is correct
12 Correct 685 ms 11872 KB Output is correct
13 Correct 682 ms 12428 KB Output is correct
14 Correct 583 ms 13652 KB Output is correct
15 Correct 589 ms 13408 KB Output is correct
16 Correct 730 ms 13584 KB Output is correct
17 Correct 754 ms 13256 KB Output is correct
18 Correct 751 ms 13384 KB Output is correct
19 Correct 741 ms 13404 KB Output is correct
20 Correct 715 ms 12756 KB Output is correct
21 Correct 714 ms 12636 KB Output is correct
22 Correct 702 ms 13284 KB Output is correct
23 Correct 581 ms 12508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 957 ms 12008 KB Output is correct
2 Correct 964 ms 11996 KB Output is correct
3 Correct 984 ms 12024 KB Output is correct
4 Correct 944 ms 11976 KB Output is correct
5 Correct 940 ms 12144 KB Output is correct
6 Correct 1396 ms 11748 KB Output is correct
7 Correct 1289 ms 11696 KB Output is correct
8 Correct 1247 ms 11492 KB Output is correct
9 Correct 159 ms 8036 KB Output is correct
10 Correct 1067 ms 11876 KB Output is correct
11 Correct 968 ms 11852 KB Output is correct
12 Correct 671 ms 11408 KB Output is correct
13 Correct 956 ms 13724 KB Output is correct
14 Correct 819 ms 10872 KB Output is correct
15 Correct 725 ms 9452 KB Output is correct
16 Correct 989 ms 11044 KB Output is correct
17 Correct 834 ms 11108 KB Output is correct
18 Correct 166 ms 8036 KB Output is correct
19 Correct 872 ms 10844 KB Output is correct
20 Correct 746 ms 10908 KB Output is correct
21 Correct 731 ms 10876 KB Output is correct
22 Correct 513 ms 10284 KB Output is correct
23 Correct 479 ms 10248 KB Output is correct
24 Correct 729 ms 11744 KB Output is correct
25 Correct 643 ms 11876 KB Output is correct
26 Correct 995 ms 12100 KB Output is correct
27 Correct 1145 ms 12000 KB Output is correct
28 Correct 1014 ms 11924 KB Output is correct
29 Correct 777 ms 11848 KB Output is correct
30 Correct 1524 ms 12132 KB Output is correct
31 Correct 1192 ms 12080 KB Output is correct
32 Correct 1190 ms 11996 KB Output is correct
33 Correct 1038 ms 11864 KB Output is correct
34 Correct 1044 ms 11996 KB Output is correct
35 Correct 1027 ms 11876 KB Output is correct
36 Correct 829 ms 11924 KB Output is correct
37 Correct 843 ms 12004 KB Output is correct
38 Correct 909 ms 11928 KB Output is correct
39 Correct 761 ms 11040 KB Output is correct
40 Correct 618 ms 11028 KB Output is correct
41 Correct 604 ms 11032 KB Output is correct
42 Correct 769 ms 13668 KB Output is correct
43 Correct 791 ms 13668 KB Output is correct
44 Correct 767 ms 13668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4480 KB Output is correct
2 Correct 7 ms 4352 KB Output is correct
3 Correct 43 ms 4992 KB Output is correct
4 Correct 22 ms 4860 KB Output is correct
5 Correct 37 ms 4984 KB Output is correct
6 Correct 34 ms 4992 KB Output is correct
7 Correct 36 ms 4988 KB Output is correct
8 Correct 34 ms 4984 KB Output is correct
9 Correct 45 ms 4984 KB Output is correct
10 Correct 34 ms 4984 KB Output is correct
11 Correct 36 ms 4984 KB Output is correct
12 Correct 37 ms 4992 KB Output is correct
13 Correct 39 ms 4980 KB Output is correct
14 Correct 38 ms 4980 KB Output is correct
15 Correct 36 ms 4992 KB Output is correct
16 Correct 37 ms 4984 KB Output is correct
17 Correct 35 ms 4984 KB Output is correct
18 Correct 957 ms 12008 KB Output is correct
19 Correct 964 ms 11996 KB Output is correct
20 Correct 984 ms 12024 KB Output is correct
21 Correct 944 ms 11976 KB Output is correct
22 Correct 940 ms 12144 KB Output is correct
23 Correct 1396 ms 11748 KB Output is correct
24 Correct 1289 ms 11696 KB Output is correct
25 Correct 1247 ms 11492 KB Output is correct
26 Correct 159 ms 8036 KB Output is correct
27 Correct 1067 ms 11876 KB Output is correct
28 Correct 968 ms 11852 KB Output is correct
29 Correct 671 ms 11408 KB Output is correct
30 Correct 956 ms 13724 KB Output is correct
31 Correct 819 ms 10872 KB Output is correct
32 Correct 725 ms 9452 KB Output is correct
33 Correct 989 ms 11044 KB Output is correct
34 Correct 834 ms 11108 KB Output is correct
35 Correct 166 ms 8036 KB Output is correct
36 Correct 872 ms 10844 KB Output is correct
37 Correct 746 ms 10908 KB Output is correct
38 Correct 731 ms 10876 KB Output is correct
39 Correct 513 ms 10284 KB Output is correct
40 Correct 479 ms 10248 KB Output is correct
41 Correct 729 ms 11744 KB Output is correct
42 Correct 643 ms 11876 KB Output is correct
43 Correct 681 ms 13412 KB Output is correct
44 Correct 158 ms 8036 KB Output is correct
45 Correct 171 ms 9960 KB Output is correct
46 Correct 168 ms 9960 KB Output is correct
47 Correct 608 ms 13664 KB Output is correct
48 Correct 683 ms 13536 KB Output is correct
49 Correct 600 ms 13444 KB Output is correct
50 Correct 541 ms 10620 KB Output is correct
51 Correct 530 ms 10676 KB Output is correct
52 Correct 516 ms 10856 KB Output is correct
53 Correct 683 ms 12008 KB Output is correct
54 Correct 685 ms 11872 KB Output is correct
55 Correct 682 ms 12428 KB Output is correct
56 Correct 583 ms 13652 KB Output is correct
57 Correct 589 ms 13408 KB Output is correct
58 Correct 730 ms 13584 KB Output is correct
59 Correct 754 ms 13256 KB Output is correct
60 Correct 751 ms 13384 KB Output is correct
61 Correct 741 ms 13404 KB Output is correct
62 Correct 715 ms 12756 KB Output is correct
63 Correct 714 ms 12636 KB Output is correct
64 Correct 702 ms 13284 KB Output is correct
65 Correct 581 ms 12508 KB Output is correct
66 Correct 995 ms 12100 KB Output is correct
67 Correct 1145 ms 12000 KB Output is correct
68 Correct 1014 ms 11924 KB Output is correct
69 Correct 777 ms 11848 KB Output is correct
70 Correct 1524 ms 12132 KB Output is correct
71 Correct 1192 ms 12080 KB Output is correct
72 Correct 1190 ms 11996 KB Output is correct
73 Correct 1038 ms 11864 KB Output is correct
74 Correct 1044 ms 11996 KB Output is correct
75 Correct 1027 ms 11876 KB Output is correct
76 Correct 829 ms 11924 KB Output is correct
77 Correct 843 ms 12004 KB Output is correct
78 Correct 909 ms 11928 KB Output is correct
79 Correct 761 ms 11040 KB Output is correct
80 Correct 618 ms 11028 KB Output is correct
81 Correct 604 ms 11032 KB Output is correct
82 Correct 769 ms 13668 KB Output is correct
83 Correct 791 ms 13668 KB Output is correct
84 Correct 767 ms 13668 KB Output is correct
85 Correct 1059 ms 15308 KB Output is correct
86 Correct 198 ms 10184 KB Output is correct
87 Correct 208 ms 10236 KB Output is correct
88 Correct 1199 ms 15332 KB Output is correct
89 Correct 1094 ms 15328 KB Output is correct
90 Correct 1022 ms 15200 KB Output is correct
91 Correct 1010 ms 12240 KB Output is correct
92 Correct 1006 ms 12252 KB Output is correct
93 Correct 1276 ms 11632 KB Output is correct
94 Correct 1153 ms 13432 KB Output is correct
95 Correct 1120 ms 13412 KB Output is correct
96 Correct 1213 ms 12892 KB Output is correct
97 Correct 744 ms 13832 KB Output is correct
98 Correct 1004 ms 15288 KB Output is correct
99 Correct 1168 ms 15328 KB Output is correct
100 Correct 1135 ms 15336 KB Output is correct
101 Correct 1186 ms 15600 KB Output is correct
102 Correct 1139 ms 15328 KB Output is correct
103 Correct 1256 ms 14816 KB Output is correct
104 Correct 1373 ms 14692 KB Output is correct
105 Correct 1146 ms 14724 KB Output is correct
106 Correct 1055 ms 14440 KB Output is correct
107 Correct 877 ms 13044 KB Output is correct
108 Correct 1230 ms 15092 KB Output is correct
109 Correct 1143 ms 13248 KB Output is correct