【2019牛客暑期多校训练营(第三场)- A】Graph Games(思维,对边分块)

题干:

链接:https://ac.nowcoder.com/acm/contest/883/A
来源:牛客网
 

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:

  1.  (1,l,r)\ (1,l,r) (1,l,r)-- reverse the status of edges numbered between  l\ l l  to  r\ r r  (1≤l≤r≤M)(1 \leq l \leq r \leq M)(1≤l≤r≤M) . i.e. Delete the edge if it is in the graph, otherwise add it to the graph. 
  2.  (2,u,v)\ (2,u,v) (2,u,v) -- ask whether  S(u)\ S(u) S(u)   and  S(v)\ S(v) S(v)   are exactly the same (1≤u≤v≤N)(1 \leq u \leq v \leq N)(1≤u≤v≤N).


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

输入

复制

1
5 4
1 2
1 3
4 2
4 3
3
2 1 4
1 1 1
2 1 2

输出

复制

10

题目大意:

一个n个点m条边的的图,2种操作:
1 l r 表示从读入的编号为l的边到第r条边,如果有这条边就删掉,否则就加上
2 x y 表示问与点x直接相连的点集是否与y直接相连的点集相同

解题报告:

Hash数组是每个点映射成一个值。

L 和 R 数组对应每个块的左右编号。

O代表单点更新的点的Hash值,laz代表整块的翻转次数的奇偶数。

B数组是预处理数组,在操作的时候不更新,预处理每个块中的每个点向外连边的Hash值。

将输入的边编号然后对边分块,如果要反转的两个区间不在相邻块内或者相同块,那么暴力l块和暴力r块,块间使用laz标记是否翻转。很优秀的想法。

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair 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 ;
}

 

你可能感兴趣的:(牛客网)