日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2019牛客暑期多校训练营(第三场)- A】Graph Games(思维,对边分块) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

鏈接:https://ac.nowcoder.com/acm/contest/883/A
來源:??途W
?

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,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,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<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 ; }

    ?

    總結

    以上是生活随笔為你收集整理的【2019牛客暑期多校训练营(第三场)- A】Graph Games(思维,对边分块)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。