【2019牛客暑期多校训练营(第三场)- A】Graph Games(思维,对边分块)
題干:
鏈接:https://ac.nowcoder.com/acm/contest/883/A
來源:??途W(wǎng)
?
You are given an undirected graph with ?N\ N?N?vertices and ?M\ M?M? edges. The edges are numbered from ?1\ 1?1?to ?M\ M?M??. Denote the set ?S(x)\ S(x)?S(x)??as: All the vertices we can reach from vertex ?x\ x?x?? by exactly?one edge.
?
You are supposed to deal with ?Q\ Q?Q?operations of the following two types:
Note that all the ?M\ M?M??edges are in the graph at the beginning.
?
輸入描述:
The input contains multiple cases. The first line of the input contains a single positive integer ?T\ T?T?, the number of cases.For each case, the first line contains two integers N?(1≤N≤100000)N\ (1 \leq N \leq 100000)N?(1≤N≤100000)??and M?(1≤M≤200000)M\ (1 \leq M \leq 200000)M?(1≤M≤200000), the number of vertices and edges in the graph. In the following ?M\ M?M??lines, the ?i\ i?i-th line contains two integers ui,vi?(1≤ui,vi≤N)u_i,v_i \ (1 \le u_i,v_i \le N)ui?,vi??(1≤ui?,vi?≤N)?, describing the the ?i\ i?i-th edge (ui,vi)(u_i,v_i)(ui?,vi?)?. Each edge appears in the input at most once. The ?(M+2)\ (M+2)?(M+2)-th line contains a integer Q?(1≤Q≤200000)Q\ (1 \leq Q \leq 200000)Q?(1≤Q≤200000)?, the number of operations. In the following ?Q\ Q?Q? lines, each line contains three integers, describing an operation.The total sum of ?N\ N?N?over all cases does not exceed ?150000\ 150000?150000. The total sum of ?M\ M?Mover all cases does not exceed ?800000\ 800000?800000. The total sum of ?Q\ Q?Q??over all cases does not exceed ?600000\ 600000?600000.輸出描述:
For each case, print a string in a line. The length of the string should equal the number of operations of type ?2\ 2?2. If the answer is yes, the ?i\ i?i-th character of the string should be `1', otherwise it should be `0'. Check the samples for more details.
示例1
輸入
復(fù)制
1 5 4 1 2 1 3 4 2 4 3 3 2 1 4 1 1 1 2 1 2輸出
復(fù)制
10題目大意:
一個n個點m條邊的的圖,2種操作:
1 l r 表示從讀入的編號為l的邊到第r條邊,如果有這條邊就刪掉,否則就加上
2 x y 表示問與點x直接相連的點集是否與y直接相連的點集相同
解題報告:
Hash數(shù)組是每個點映射成一個值。
L 和 R 數(shù)組對應(yīng)每個塊的左右編號。
O代表單點更新的點的Hash值,laz代表整塊的翻轉(zhuǎn)次數(shù)的奇偶數(shù)。
B數(shù)組是預(yù)處理數(shù)組,在操作的時候不更新,預(yù)處理每個塊中的每個點向外連邊的Hash值。
將輸入的邊編號然后對邊分塊,如果要反轉(zhuǎn)的兩個區(qū)間不在相鄰塊內(nèi)或者相同塊,那么暴力l塊和暴力r塊,塊間使用laz標記是否翻轉(zhuǎn)。很優(yōu)秀的想法。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1e5 + 5; int blk[MAX]; int a[MAX<<1],b[MAX<<1]; int n,m,q,tot,Block; int L[505],R[505],B[505][MAX],laz[505],O[MAX],Hash[MAX]; int main() {for(int i = 1; i<=1e5; i++) Hash[i] = rand(); int t;cin>>t;while(t--) {tot=0;scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) O[i] = 0;for(int i = 1; i<=m; i++) cin>>a[i]>>b[i];Block = sqrt(m);for(int i = 1; i<=m; i+=Block) {L[++tot] = i;R[tot] = min(m,i+Block-1);for(int j = 1; j<=n; j++) B[tot][j] = 0;for(int j = L[tot]; j<=R[tot]; j++) B[tot][a[j]] ^= Hash[b[j]],B[tot][b[j]] ^= Hash[a[j]];laz[tot] = 0; }scanf("%d",&q);while(q--) {int op,l,r; scanf("%d%d%d",&op,&l,&r);if(op == 2) {int x = O[l],y = O[r];for(int i = 1; i<=tot; i++) {if(!laz[i]) x ^= B[i][l],y ^= B[i][r];}printf("%d",x == y);}else {int B1 = (l+Block-1)/Block,B2 = (r+Block-1)/Block;if(B2-B1>=2) {for(int i = B1+1; i<=B2-1; i++) laz[i] ^= 1;for(int i = l; i<=R[B1]; i++) O[a[i]] ^= Hash[b[i]],O[b[i]] ^= Hash[a[i]];for(int i = L[B2]; i<=r; i++) O[a[i]] ^= Hash[b[i]],O[b[i]] ^= Hash[a[i]];}else {for(int i = l; i<=r; i++) O[a[i]] ^= Hash[b[i]],O[b[i]] ^= Hash[a[i]];} }}puts("");}return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【2019牛客暑期多校训练营(第三场)- A】Graph Games(思维,对边分块)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WinAce.exe - WinAce是
- 下一篇: 华为启动鸿蒙开发者大赛 奖金高达100万