Description

有n对夫妇,一开始夫妇之间互不认识,若两男或两女成为朋友,称他们为"熟人","熟人"关系具有传递性,即若a熟b且b熟c则a熟c.若两组夫妇的丈夫互相为熟人且妻子也相互为熟人则称他们为"熟悉的一对",现在给出q个事件,每个事件会使得两男或两女成为朋友,并在每次事件之后计算"熟悉的一对"的个数.

Input

第一行一个数T表示数据组数
接下来n,q表示对数和事件数
接下来q行,每行t,a,b,若t=1,表示男a和男b成为朋友,t=2,表示女a和女b成为朋友

Output

设当前是第i个操作,y_i为本次事件之后的答案,令z_i=i*y_i,请输出z_1+z_2+...+z_q模10^9+7
题意简化为维护两个图,支持在一个图中连边和询问有多少点对在两个图中都连通
用两个并查集分别维护两个图中的连通性,用一个hashmap维护一个n*n的二维数组,f[x][y]表示在并查集1中属于集合x,在并查集2中属于集合y的点的个数
每次在一个图中连边时,若两侧连通则忽略,不联通则遍历小的一个联通块,通过计算在另一个图中跨过两个联通块的联通块更新答案
均摊时间复杂度O(Tnlogn)
#include<cstdio>#include<vector>
const int P=1e9+,N=1e6+;
inline int read(){
int x=,c=getchar();
while(c>''||c<'')c=getchar();
while(c>=''&&c<='')x=x*+c-'',c=getchar();
return x;
}
inline void exch(int&a,int&b){int c=a;a=b;b=c;}namespace Map{
const int mx=;
unsigned int nw=;
unsigned int xs[mx],ys[mx],zs[mx],ds[mx];
inline void clear(){
nw++;
}
inline int get(unsigned int x,unsigned int y,int inc){
unsigned int w=(x*+y*+)%mx;
while(ds[w]==nw){
if(xs[w]==x&&ys[w]==y){
int v=zs[w];
zs[w]+=inc;
return v;
}
w+=;
if(w>=mx)w-=mx;
}
ds[w]=nw;
xs[w]=x;ys[w]=y;zs[w]=inc;
return ;
}
}
int T,n,m,now=,Ans,ans;
int h1[N],h2[N],f1[N],f2[N],sz1[N],sz2[N],nx1[N],nx2[N];
int t[N],d[N];
std::vector<int>v1[N],v2[N];
int main(){
T=read();
while(T--){
n=read();m=read();
Ans=ans=;
Map::clear();
for(int i=;i<=n;i++){
Map::get(i,i,);
f1[i]=f2[i]=i;
v1[i].clear();v2[i].clear();
v1[i].push_back(i);
v2[i].push_back(i);
}
for(int i=,op,a,b;i<=m;++i){
op=read();a=read();b=read();
++now;
if(op==){
if(f1[a]!=f1[b]){
if(v1[f1[a]].size()>v1[f1[b]].size())exch(a,b);
std::vector<int>&vc=v1[f1[a]];
for(int x=;x<vc.size();x++){
int p=vc[x];
int f=f2[p];
if(d[f]!=now)d[f]=now,ans=(ans+Map::get(f1[p],f,-)*1ll*Map::get(f1[b],f,)%P)%P;
else Map::get(f1[p],f,-),Map::get(f1[b],f,);
f1[p]=f1[b];
v1[f1[b]].push_back(p);
}
vc.clear();
}
}else{
if(f2[a]!=f2[b]){
if(v2[f2[a]].size()>v2[f2[b]].size())exch(a,b);
std::vector<int>&vc=v2[f2[a]];
for(int x=;x<vc.size();x++){
int p=vc[x];
int f=f1[p];
if(d[f]!=now)d[f]=now,ans=(ans+Map::get(f,f2[p],-)*1ll*Map::get(f,f2[b],)%P)%P;
else Map::get(f,f2[p],-),Map::get(f,f2[b],);
f2[p]=f2[b];
v2[f2[b]].push_back(p);
}
vc.clear();
}
}
Ans=(Ans+ans*1ll*i%P)%P;
}
printf("%d\n",Ans);
}
return ;
}

bzoj4153 [Ipsc2015]Familiar Couples的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. Sicily 1021. Couples

    题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...

  3. Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical

    http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...

  4. [LeetCode] Couples Holding Hands 两两握手

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  5. [Swift]LeetCode765. 情侣牵手 | Couples Holding Hands

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  6. [CC-COUPLES]Couples sit next to each other

    [CC-COUPLES]Couples sit next to each other 题目大意: 有\(n(n\le5\times10^5)\)对小伙伴共\(2n\)个人坐成一圈.刚开始编号为\(i\ ...

  7. 每日英语:Why Rate Your Marriage? A Numerical Score Can Help Couples Talk About Problems

    When marriage therapist Sharon Gilchrest O'Neill met with new clients recently, she asked them why t ...

  8. ZOJ 3161 Damn Couples 动态规划 难度:2

    Damn Couples Time Limit: 1 Second      Memory Limit: 32768 KB As mentioned in the problem "Coup ...

  9. LeetCode765. Couples Holding Hands

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

随机推荐

  1. 使用OpenSSL生成证书并配置Https

    1.密钥.证书请求.证书概要说明 在证书申请签发过程中,客户端涉及到密钥.证书请求.证书这几个概念.我们以申请证书的流程说明三者的关系.客户端(相对于CA)在申请证书的时候,大体上有三个步骤: 第一步 ...

  2. iOS调用第三方地图App进行导航方法

    前言 App内根据手机上装载的地图App将其显示在弹出的选择框,选择对应地图跳转进入地图导航.需要用到- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS( ...

  3. MyEclipse 2017 Stable 2.0发布|附下载

    MyEclipse个人开发者专享6.9折!仅剩最后3天!在线订购>> 2017 Stable 2.0版本带来了新的功能和修复,从Stable 1.0以来,已经发布了几个版本的Angular ...

  4. Free 4 months Serial License Key Of Outpost Security Suite Pro 8.1

    VISIT HEREto gey a 1 year key.... for the latest version Promo code ? well use the same here too : C ...

  5. HDU 3829

    http://acm.hdu.edu.cn/showproblem.php?pid=2970 P个小朋友喜欢猫讨厌狗,喜欢狗讨厌猫,移除一定数量的猫狗,使开心的小朋友数量最多 二分图最大独立集=顶点数 ...

  6. ios开发过程中描述文件(provisioning profile)过期导致ios无法正常安装的处理办法

    1.登录开发者中心,重新编辑描述文件,获得最新的描述文件.(如果对应的P12文件也过期,需要同时下载最新的p12文件).----该步骤需要有权限的人才能操作. 2.下载最新的描述文件和p12文件(如果 ...

  7. android复制包需要修改的几个地方

    1.要看什么情况,若是在同一个eclipse下,那么就需要修改包名.若不在的话,那就可以不用了. 2.这个app_name也是一样. 3.先说下情况,这是我修改好的.原本这个R的是引之前包的,必须要改 ...

  8. 本地和服务器(ubuntu)文件同步

    秘钥登录远端服务器 rsync -avze 'ssh -i ./id_rsa' root@remoteIp:/xx/remotefile.txt ./localpath (./id_rsa为本地秘钥路 ...

  9. js窗口拖动 模版

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  10. 《DSP using MATLAB》Problem 4.2

    用matlab不会证,惭愧.