The epidemic situation is not over yet. We should strictly prevent the recurrence of the epidemic situation. In order to do a good job in epidemic prevention and control, regional risk levels have been set in China, and personnel in medium and high-risk areas have been restricted from moving and isolated at home.
In order to study the impact of epidemic prevention and control on cross regional transportation, it is assumed that there are N An airport, M Every day, a new prevention and control area will be added to each route. A prevention and control area will cause an airport to fail to operate normally, and the route will naturally fail to operate normally. There will be accidents every day Qi For passengers from Xi Airport bound Yi Airport, please calculate how many passengers will be affected and unable to complete the trip.
As long as passengers can go directly or through several transfers, and the departure and arrival airports of all routes are in normal operation, they are deemed to be able to complete the journey.
Input format:
The first line of input is three integers N,M,D (1≤N≤5 × 104, 1≤M≤2 × 105, 1 ≤ D ≤ 103), indicating the number of airports, routes and days of new prevention and control areas.
Next, first M Lines, each line gives two numbers separated by spaces A and B. Indicates that the number is A and B There is a route between airports. The route is two-way, and the airport number is from 1 to N.
And then D Block input, two integers separated by spaces in the first line of each block input C and Q (1 ≤ Q ≤ 103), indicating that the new airport number is C The city is a prevention and control area. Today there are Q Segment itinerary. The data ensure that the new city must not be a prevention and control area before.
Next Q Lines, each line is two numbers separated by spaces X and Y. Indicates that the number is X and Y A trip to the airport. The trip may include cities that have previously become prevention and control areas.
Output format:
For daily inquiries, please output in one line the number of itineraries that cannot be made on the day after a new prevention and control area is added.
Input example:
5 5 3 1 2 1 3 1 5 2 5 3 4 4 3 1 3 1 4 2 3 5 3 3 4 2 3 3 5 1 3 2 3 2 5 3 4
Output example:
1 2 3
Idea:
The meaning of the induction question is to give a diagram that supports two operations:
1) Delete a point.
2) Ask whether the two points are connected.
It is observed that the "delete" operation cannot be maintained simply. Consider taking all operations offline and processing them in reverse order. That is, first delete all the points to be deleted, then add points backwards, and then dispose of the queries and use them for query and maintenance.
The time complexity is O(n+mlogn+qlogn).
#include<iostream> #include<stack> #include<vector> using namespace std; const int N = 50010, M = 200010, Q = 1010; int n,m,d; int f[N]; bool book[N];//Is the point deleted struct Edge{ int u,v; }; vector<int> g[N];//Adjacency table vector<Edge> query;//Query offline vector<int> alg,erap;//alg number of queries era s deleted points stack<int> res;//result int getf(int v){ if(f[v] == v) return v; else return f[v] = getf(f[v]); } void merge(int a,int b){ int t1 = getf(a); int t2 = getf(b); if(t1 != t2){ f[t2] = t1; } } bool ismerge(int a,int b){ return getf(a) == getf(b); } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n,m,d; cin>>n>>m>>d; for(int i = 1; i <= n; i++) f[i] = i; //Offline operation for(int i = 0 ; i < m; i++){ int u,v; cin>>u>>v; g[v].push_back(u); g[u].push_back(v); } for(int i = 0; i < d; i++){ int c,q; cin>>c>>q; book[c] = true; erap.push_back(c); alg.push_back(q); for(int j = 0; j < q; j++){ int u,v; cin>>u>>v; query.push_back({u,v}); } } //Generate and query the relevant points that have not been deleted for(int i = 1; i <= n; i++){ if(!book[i]){ for(auto p:g[i]){ if(!book[p]) merge(i,p); } } } for(int i = alg.size()-1; i >= 0; i--){ int count = 0; for(int j = 0; j < alg[i]; j++){ Edge tmp = query[query.size()-1]; if(!ismerge(tmp.u,tmp.v)){ count++; } if(query.size()) query.pop_back(); } res.push(count); //insertion point int p = erap[i]; book[p] = false; for(int x:g[p]){ if(!book[x]) merge(x,p); } } while(res.size()){ cout<<res.top(); res.pop(); if(res.size()) cout<<'\n'; } return 0; }