#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. Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)

    P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...

  2. 向Linus学习,让代码具有good taste

    在最近关于 Linus Torvalds 的一个采访中,这位 Linux 的创始人,在采访过程中大约 14:20 的时候,提及了关于代码的 “good taste”.good taste?采访者请他展 ...

  3. Spring-security自定义过滤器

    定义过滤器 public class TokenAuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter { publ ...

  4. 一个Velocity Template Language学习的框架

    Velocity Template Language(VTL)使得数据展示和后台代码的开发分离开来,最初用在基于servlet的网站开发上,它的这一特性使得它在应付MVC Web开发模式时显得尤其合适 ...

  5. C# 访问mongodb数据库

    1.引用四个mongodb动态库MongoDB.Bson.dll,MongoDB.Driver.Core.dll,MongoDB.Driver.dll,MongoDB.Driver.Legacy.dl ...

  6. jQuery学习笔记(3)-操作jQuery包装集的函数

    一.前言 在使用jQuery选择器获取到jQuery包装集后,我们就要对这些包装集进行各种操作 二.创建新的元素 1.使用HTMLDOM创建元素 (1)什么是DOM 当网页被加载时,浏览器会创建页面的 ...

  7. Spring框架学习-Spring和IOC概述

    一:什么是Spring框架? spring是一个分层的javase/EEfull-stack(一站式)轻量级的java开源框架.是为了解决企业开发的复杂性而创建的.框架的主要优势是分层架构,Sprin ...

  8. springboot与dubbo整合入门(三种方式)

    Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...

  9. 动态属性ExpandoObject

    1.动态创建对象及其属性ExpandoObject 查看ExpandoObject的定义:

  10. swiper3初始化/swiper-init/用data实例化swiper/data-swiper

    Framework7直接用data属性实例化swiper用起来很爽,刚好最近又用到swiper插件,自己写一个 HTML <div class="swiper-container sw ...