Submission #245764

#TimeUsernameProblemLanguageResultExecution timeMemory
245764icypiggyBridges (APIO19_bridges)C++14
100 / 100
1390 ms15712 KiB
#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) { x = findset(x); y = findset(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 (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:149:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<breakpoints.size(); i++) {
                  ~^~~~~~~~~~~~~~~~~~~
bridges.cpp:154:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<output.size(); i++) {
                  ~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...