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 1233: [Usaco2009Open]干草堆tower【dp+单调栈】
参考:https://www.cnblogs.com/N-C-Derek/archive/2012/07/11/usaco_09_open_tower.html 虽然长得很像斜率优化,但是应该不算-- ...
- knockout jquery警告删除
//触发删除的动作 $("a.delete").live('click', function () { var ...
- curl 做爬虫 用服务器代理ip
有时候会ip会封锁,所以会用一些模拟代理ip进行抓取测试 从网上找了一下代码 function curl_string ($url,$user_agent,$proxy){ $ch = ...
- maxItemsInObjectGraph解释
maxItemsInObjectGraph:一个整数,指定要序列化或反序列化的最大项数,可以限制对象图中要序列化的项数.默认的就是65535,当客户端与WebService之间传递的是对象要序列化的个 ...
- SQL编程语句
视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名 as SQL查询语句,分组,排序,in 等都不能写视图的用法: select * from 视图名 SQL编程 定义变量:d ...
- 专题十一:实现一个基于FTP协议的程序——文件上传下载器
引言: 在这个专题将为大家揭开下FTP这个协议的面纱,其实学习知识和生活中的例子都是很相通的,就拿这个专题来说,要了解FTP协议然后根据FTP协议实现一个文件下载器,就和和追MM是差不多的过程的,相信 ...
- 专题四:自定义Web浏览器
前言: 前一个专题介绍了自定义的Web服务器,然而向Web服务器发出请求的正是本专题要介绍的Web浏览器,本专题通过简单自定义一个Web浏览器来简单介绍浏览器的工作原理,以及帮助一些初学者揭开浏览器这 ...
- JavaScript Json与Map互转以及Map对象的取值方式
Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...
- java死锁问题
一.先从定义上了解一下死锁 二.从代码角度上去解释一下死锁问题 三.上述程序就是出现了死锁,我们来查看一下 1.命令如下 cmd>>jps(查看到了死锁线程所在的类,前面是PID) 2 ...
- CAD使用SetxDataDouble写数据(网页版)
主要用到函数说明: MxDrawEntity::SetxDataDouble 写一个Double扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据 ...