BZOJ3569: DZY Loves Chinese II(线性基构造)

Description

神校XJ之学霸兮,Dzy皇考曰JC。
摄提贞于孟陬兮,惟庚寅Dzy以降。
纷Dzy既有此内美兮,又重之以修能。
遂降临于OI界,欲以神力而凌♂辱众生。
 
今Dzy有一魞歄图,其上有N座祭坛,又有M条膴蠁边。
时而Dzy狂WA而怒发冲冠,神力外溢,遂有K条膴蠁边灰飞烟灭。
而后俟其日A50题则又令其复原。(可视为立即复原)
然若有祭坛无法相互到达,Dzy之神力便会大减,于是欲知其是否连通。

Input

第一行N,M
接下来M行x,y:表示M条膴蠁边,依次编号
接下来一行Q
接下来Q行:
每行第一个数K而后K个编号c1~cK:表示K条边,编号为c1~cK
为了体现在线,c1~cK均需异或之前回答为连通的个数

Output

对于每个询问输出:连通则为‘Connected’,不连通则为‘Disconnected’
(不加引号)

Sample Input

5 10
2 1
3 2
4 2
5 1
5 3
4 1
4 3
5 2
3 1
5 4
5
1 1
3 7 0 3
4 0 7 4 6
2 2 7
4 5 0 2 13

Sample Output

Connected
Connected
Connected
Connected
Disconnected

解题思路:

考虑到将原图不连通必须切断一个点的所有联通方式

那么可以想到用一种方式来使多个元素互相抵消。

这些元素就是连同一个点的所有边。

那么使这些遍抵消的方式就是让一条边与其他异或和为0

这就需要线性无关组了。

Dfs出一颗树。

将非树边的每一条边rand上一个权值,

那么这条边能做出贡献的位置就是Dfs树上祖先的位置。

那么就向上更新,在树边处的答案就是后面相关边的异或和

最后在查询时暴力插入线性无关组中,

若出现异或和为0的情况就是所有相关的边都被删除了。

就是不连通了。

代码:

  1 #include<ctime>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<algorithm>
  6 void ade(int f,int t,int no);
  7 typedef long long lnt;
  8 struct pnt{
  9     int hd;
 10     int fa;
 11     lnt val;
 12 }p[100010];
 13 struct ent{
 14     int twd;
 15     int lst;
 16     int blg;
 17 }e[3000000];
 18 struct edge{
 19     int f,t;
 20     lnt val;
 21     bool vis;
 22     void Insert(int no)
 23     {
 24         scanf("%d%d",&f,&t);
 25         ade(f,t,no);
 26         ade(t,f,no);
 27     }
 28 }ede[1000000];
 29 lnt b[65];
 30 int n,m;
 31 int cnt;
 32 int Q;
 33 int lstans;
 34 void ade(int f,int t,int no)
 35 {
 36     cnt++;
 37     e[cnt].twd=t;
 38     e[cnt].blg=no;
 39     e[cnt].lst=p[f].hd;
 40     p[f].hd=cnt;
 41     return ;
 42 }
 43 void B_(int x,int f)
 44 {
 45     p[x].fa=f;
 46     for(int i=p[x].hd;i;i=e[i].lst)
 47     {
 48         int to=e[i].twd;
 49         if(p[to].fa)continue;
 50         ede[e[i].blg].vis=true;
 51         B_(to,x);
 52     }
 53     return ;
 54 }
 55 void C_(int x,int f)
 56 {
 57     for(int i=p[x].hd;i;i=e[i].lst)
 58     {
 59         int to=e[i].twd;
 60         if(p[to].fa==x);else continue;
 61         C_(to,x);
 62         ede[e[i].blg].val^=p[to].val;
 63         p[x].val^=p[to].val;
 64     }
 65     return ;
 66 }
 67 bool Insert(lnt x)
 68 {
 69     for(int i=62;i>=0;i--)
 70     {
 71         if(x&(1ll<<i))
 72         {
 73             if(b[i]==0)
 74             {
 75                 b[i]=x;
 76                 return true;
 77             }else x^=b[i];
 78         }
 79     }
 80     if(!x)return false;
 81     return true;
 82 }
 83 int main()
 84 {
 85 //    freopen("a.in","r",stdin);
 86     srand(time(NULL));
 87     scanf("%d%d",&n,&m);
 88     for(int i=1;i<=m;i++)ede[i].Insert(i);
 89     B_(1,1);
 90     for(int i=1;i<=m;i++)
 91     {
 92         if(ede[i].vis)continue;
 93         ede[i].val=rand()+1;
 94         p[ede[i].f].val^=ede[i].val;
 95         p[ede[i].t].val^=ede[i].val;
 96     }
 97     C_(1,1);
 98     scanf("%d",&Q);
 99     while(Q--)
100     {
101         int k;bool flag=false;
102         scanf("%d",&k);
103         memset(b,0,sizeof(b));
104         for(int i=1;i<=k;i++)
105         {
106             int x;
107             scanf("%d",&x);x^=lstans;
108             if(!Insert(ede[x].val))flag=true;
109         }
110         if(flag)
111         {
112             puts("Disconnected");
113         }else{
114             puts("Connected");
115             lstans++;
116         }
117     }
118     return 0;
119 }
原文地址:https://www.cnblogs.com/blog-Dr-J/p/10306395.html