2017北京ICPC C题 Graph
#1629 : Graph
描述
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的更多相关文章
- 2017北京国庆刷题Day1 afternoon
期望得分:100+100+100=300 实际得分:100+100+100=300 T1 一道图论好题(graph) Time Limit:1000ms Memory Limit:128MB 题目 ...
- 2017北京国庆刷题Day7 morning
期望得分:100+0+100=200 实际得分:100+20+0=120 离散化搞搞 #include<cstdio> #include<iostream> #include& ...
- 2017北京国庆刷题Day5 afternoon
期望得分:100+60+100=260 实际得分:0+60+40=100 设图中有m个环,每个环有si条边,有k条边不在环中 ans= (2^s1 -2)*( 2^s2 -2)* (2^s3 -2)… ...
- 2017北京国庆刷题Day3 morning
期望得分:100+60+0=160 实际得分:100+30+0=130 考场上用的哈希 #include<cstdio> #include<cstring> #include& ...
- 2017北京国庆刷题Day2 afternoon
期望得分:100+100+50=250 实际得分:100+70+50=220 T1 最大值(max) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一 ...
- 2017北京国庆刷题Day2 morning
期望得分:100+100+40=240 实际得分:100+40+0=140 T1 一道图论神题(god) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK ...
- 2017北京国庆刷题Day4 morning
期望得分:0+40+30=70 实际得分:0+10+10=20 题目修改:只能由0变1,只能用一次操作 大模拟 #include<cstdio> #include<cstring&g ...
- 2017北京国庆刷题Day5 morning
期望得分:0+60+60=120 实际得分:0+30+60=90 令g=gcd(X11,X12,X13……) 则行列式可能为D的充要条件为g|D 1.g|D为必要条件: 由定义来算行列式的时候,每一项 ...
- 2017北京国庆刷题Day4 afternoon
期望得分:100+100+0=200 实际得分:5+0+0=5 每加入一个数,x的因数位置++ 注意:根号x枚举时,如果x是完全平方数,根号x会重复累计2次,要减去 考场上没减,5分 /(ㄒoㄒ)/~ ...
随机推荐
- bzoj 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典【dp】
预处理出g[i][j]表示原串第i个匹配第j个单词需要去掉几个字母(匹配不上为-1) 设f[i]为i及之后满足条件要去掉的最少字母 倒着dp! f[i]初始为f[i+1]+1,转移方程为f[i]=mi ...
- RT-Thread 设备驱动-硬件定时器浅析与使用
RT-Thread 4.0.0 访问硬件定时器设备 应用程序通过 RT-Thread 提供的 I/O 设备管理接口来访问硬件定时器设备,相关接口如下所示: 函数 描述 rt_device_find() ...
- hihocoder #1698 假期计划 (排列组合+费马小定理+乘法逆元)
Description 小Ho未来有一个为期N天的假期,他计划在假期中看A部电影,刷B道编程题.为了劳逸结合,他决定先拿出若干天看电影,再拿出若干天刷题,最后再留若干天看电影.(若干代指大于0) 每 ...
- win7任务计划提示”该任务映像已损坏或已篡改“怎么处理
https://jingyan.baidu.com/article/e75057f2038e2febc91a8915.html 在命令行窗口(cmd)执行命令:schtasks /query /v ...
- [转]mysql的查询、子查询及连接查询
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html 一.mysql查询的五种子句 where(条件 ...
- linux小白成长之路5————安装Docker
1.安装docker 命令: yum -y install docker   2.启动docker 命令: systemctl start docker.service 3.查看docker版本 ...
- ios基础笔试题-集锦二
前言 下文转载自:http://www.henishuo.com/objc-interview-two/ 1.即时聊天App不会采用的网络传输方式 A. UDP B. TCP C. HTTP D. F ...
- 重构27-Remove God Classes(去掉神类)
在传统的代码库中,我们常常会看到一些违反了SRP原则的类.这些类通常以Utils或Manager结尾,有时也没有这么明显的特征而仅仅是普通的包含多个功能的类.这种God类还有一个特征,使用语句或注释将 ...
- (转)淘淘商城系列——商品搜索功能Dao实现
http://blog.csdn.net/yerenyuan_pku/article/details/72909286 终于进入商品搜索功能的开发中了,本文我来教大家编写实现商品搜索功能的Dao层代码 ...
- HDU_1011_Starship Troopers_树型dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Jav ...