#1629 : Graph

时间限制:4000ms
单点时限:4000ms
内存限制:256MB

描述

The country contains N cities numbered from 1 to N and M undirected roads connecting pairs of cities. There are some queries. Each query is represented by two numbers: L and R , meaning that all the cities whose number is between L and R(L and R are included) are safe, and other cities are not safe. We define city A can reach city B if and only if they are both safe and there exists a path from A to B that the cities on the path are all safe.

For each query, you need to figure out the number of pairs of cities that can reach each other under the condition given by the query.

输入

First line contains one number T which means the number of test cases.

For each test case, first line contains three numbers, above mentioned N , M and Q.

Next M lines, each line contains two integers: X, Y (X != Y) which means there is a road between city X and city Y (1 <= X,Y <= N).

Next Q lines, each line contains two numbers: L, R which indicates an query(1 <= L,R <= N, L <= R).

T <= 5, N , M <= 50000, Q <= 100000

输出

For each test case, output Q lines, each line contains the answer of the correspondent query.

样例输入
1
6 6 4
1 2
2 3
2 6
1 5
2 4
4 5
1 4
3 6
2 6
3 4
样例输出
6
1
10
0
分析:对于询问(L,R),可行的边(x,y)满足L<=x<=y<=R;
   那么分成两部分,满足L<=x的边与y<=R的边的交即可;
   于是把边拷贝两份分别排序求前缀交,可以离线用回滚莫队转移;
   维护答案用并查集即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 19260817
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=1e5+;
const int N=2e5;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,q,num,bl[maxn],fa[maxn],sz[maxn],cnt[][maxn],tot,L[maxn],R[maxn];
ll ret,lst,ans[maxn];
struct node
{
int u,v,id;
bool operator<(const node&p)const
{
return bl[u]==bl[p.u]?v<p.v:bl[u]<bl[p.u];
}
}e[maxn],f[maxn],qu[maxn],tmp[maxn];
bool cmp1(node x,node y)
{
return x.u>y.u;
}
bool cmp2(node x,node y)
{
return x.v<y.v;
}
int find(int x){return fa[x]==x?x:find(fa[x]);}
void unite(int x,int y)
{
x=find(x),y=find(y);
if(x==y)return;
if(sz[x]>sz[y])swap(x,y);
fa[x]=y;
ret+=(ll)sz[y]*sz[x];
sz[y]+=sz[x];
}
void undo()
{
while(tot)
{
int x=tmp[tot].u,y=tmp[tot].v;
fa[x]=x;
sz[y]-=sz[x];
ret-=(ll)sz[y]*sz[x];
tot--;
}
}
int main(){
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&q);
rep(i,,m)
{
int x,y;
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
e[i]=f[i]=node{x,y,i};
}
sort(e+,e+m+,cmp1);
sort(f+,f+m+,cmp2);
int pos=;
for(i=n;i>=;i--)
{
while(pos+<=m&&e[pos+].u>=i)pos++;
L[i]=pos;
}
pos=;
rep(i,,n)
{
while(pos+<=m&&f[pos+].v<=i)pos++;
R[i]=pos;
}
num=round(sqrt(m)+0.5);
rep(i,,m)bl[i]=(i-)/num+;
rep(i,,q)
{
int x,y;
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
qu[i]=node{L[x],R[y],i};
}
sort(qu+,qu+q+);
int l,r;
rep(i,,q)
{
if(i==||bl[qu[i].u]!=bl[qu[i-].u])
{
rep(j,,n)fa[j]=j,sz[j]=;
rep(j,,m)cnt[][j]=cnt[][j]=;
ret=tot=;
j=;
while(j<=m&&j<=bl[qu[i-].u]*num)++cnt[][e[j++].id];
l=j,r=;
}
while(r<=m&&r<=qu[i].v)
{
if(++cnt[][f[r].id]==&&cnt[][f[r].id])
{
unite(f[r].u,f[r].v);
}
r++;
}
lst=ret;
while(l<=m&&l<=qu[i].u)
{
if(++cnt[][e[l].id]==&&cnt[][e[l].id])
{
int x=find(e[l].u),y=find(e[l].v);
if(x!=y)
{
if(sz[x]>sz[y])swap(x,y);
tmp[++tot]=node{x,y,};
unite(x,y);
}
}
l++;
}
while(l>j)
{
--cnt[][e[--l].id];
}
ans[qu[i].id]=ret;
undo();
ret=lst;
}
rep(i,,q)printf("%lld\n",ans[i]);
}
return ;
}
/*
1
3 3 6
1 2
2 3
3 1
1 1
2 2
3 3
1 2
2 3
1 3
*/

2017北京ICPC C题 Graph的更多相关文章

  1. 2017北京国庆刷题Day1 afternoon

    期望得分:100+100+100=300 实际得分:100+100+100=300 T1 一道图论好题(graph) Time Limit:1000ms   Memory Limit:128MB 题目 ...

  2. 2017北京国庆刷题Day7 morning

    期望得分:100+0+100=200 实际得分:100+20+0=120 离散化搞搞 #include<cstdio> #include<iostream> #include& ...

  3. 2017北京国庆刷题Day5 afternoon

    期望得分:100+60+100=260 实际得分:0+60+40=100 设图中有m个环,每个环有si条边,有k条边不在环中 ans= (2^s1 -2)*( 2^s2 -2)* (2^s3 -2)… ...

  4. 2017北京国庆刷题Day3 morning

    期望得分:100+60+0=160 实际得分:100+30+0=130 考场上用的哈希 #include<cstdio> #include<cstring> #include& ...

  5. 2017北京国庆刷题Day2 afternoon

    期望得分:100+100+50=250 实际得分:100+70+50=220 T1 最大值(max) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一 ...

  6. 2017北京国庆刷题Day2 morning

    期望得分:100+100+40=240 实际得分:100+40+0=140 T1 一道图论神题(god) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK ...

  7. 2017北京国庆刷题Day4 morning

    期望得分:0+40+30=70 实际得分:0+10+10=20 题目修改:只能由0变1,只能用一次操作 大模拟 #include<cstdio> #include<cstring&g ...

  8. 2017北京国庆刷题Day5 morning

    期望得分:0+60+60=120 实际得分:0+30+60=90 令g=gcd(X11,X12,X13……) 则行列式可能为D的充要条件为g|D 1.g|D为必要条件: 由定义来算行列式的时候,每一项 ...

  9. 2017北京国庆刷题Day4 afternoon

    期望得分:100+100+0=200 实际得分:5+0+0=5 每加入一个数,x的因数位置++ 注意:根号x枚举时,如果x是完全平方数,根号x会重复累计2次,要减去 考场上没减,5分 /(ㄒoㄒ)/~ ...

随机推荐

  1. 1617: [Usaco2008 Mar]River Crossing渡河问题(dp)

    1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1219  Solved:  ...

  2. bzoj1528 sam-Toy Cars(贪心,优先队列)

    「BZOJ1528」[POI2005] sam – Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Ja ...

  3. (数论)51NOD 1073 约瑟夫环

    N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数.问最后剩下的人的编号.例如:N = 3,K = 2.2号先出列,然后是1号,最后剩下的是3号.In ...

  4. hdu2031

    http://acm.hdu.edu.cn/showproblem.php?pid=2031 #include<stdio.h> #include<math.h> #inclu ...

  5. 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers

    题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...

  6. SQL server 查询语句 练习题

    用SQL语句创建四个表: create database tongjigouse tongjigocreate table student(Sno varchar(20) not null prima ...

  7. 微信小程序组件解读和分析:四、icon图标

      icon图标组件说明: icon是一种图标格式,用于系统图标.软件图标等,这种图标扩展名为.icon..ico.常见的软件或windows桌面上的那些图标一般都是ICON格式的.在应用上面很多地方 ...

  8. Linux 配置 nginx + php

    为什么!!!我配过的服务器已经有5.6个了吧,为什么每一次配置都能要了我的老命??这次写清楚过程,以后再要被配服务器坑,我特么要砍人了. 提示:测试网站能否访问的时候,最好关掉浏览器的缓存功能或者勤清 ...

  9. 在Resource中使用x:Bind

    Build2015上,MS热情高涨的演示了x:Bind,一种新的Binding方式,新的方式有如下优点: 1更好的性能(内存占用,CPU占用) 2BuildTime的Binding 具体在Channe ...

  10. margin与padding如何进行区分

    margin与padding如何进行区分,这是很多学html人的困扰,其实说白了padding 就是内容与边框的空隙.而margin则是模块与模块的空隙.[3]