列表

详情


NC51610. Explorer

描述

Gromah and LZR have entered the fifth level. Unlike the first four levels, they should do some moves in this level.

There are vertices and bidirectional roads in this level, each road is in format , which means that vertex and are connected by this road, but the sizes of passers should be in interval . Since passers with small size are likely to be attacked by other animals and passers with large size may be blocked by some narrow roads.

Moreover, vertex is the starting point and vertex is the destination. Gromah and LZR should go from vertex to vertex to enter the next level.

At the beginning of their exploration, they may drink a magic potion to set their sizes to a fixed positive integer. They want to know the number of positive integer sizes that make it possible for them to go from to .

Please help them to find the number of valid sizes.

输入描述

The first line contains two positive integers , denoting the number of vertices and roads.

Following m lines each contains four positive integers , denoting a bidirectional road .



输出描述

Print a non-negative integer in a single line, denoting the number of valid sizes.

示例1

输入:

5 5
1 2 1 4
2 3 1 2
3 5 2 4
2 4 1 3
4 5 3 4

输出:

2

说明:

There are 2 valid sizes : 2 and 3.

For size 2, there exists a path 1 \rightarrow 2 \rightarrow 3 \rightarrow 5.

For size 3, there exists a path 1 \rightarrow 2 \rightarrow 4 \rightarrow 5.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 812ms, 内存消耗: 35576K, 提交时间: 2020-10-24 12:25:50

#include<bits/stdc++.h>
#include<vector>
#define lson u<<1,l,mid
#define rson u<<1|1,mid+1,r
using namespace std;
const int N=100005;
int n,m,U[N],V[N],L[N],R[N],fa[N],sz[N],ans;
vector<int>v,e[N<<3];
int find(int x){return fa[x]==x?x:find(fa[x]);}
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
void update(int u,int l,int r,int x,int y,int val){
	if(x<=l&&r<=y){
		e[u].push_back(val);
		return;
	}int mid=(l+r)>>1;
	if(x<=mid)update(lson,x,y,val);
	if(y>mid)update(rson,x,y,val);
}
void dfs(int u,int l,int r){
	vector<int>lst;int mid=(l+r)>>1;
	for(int i=0;i<(int)e[u].size();i++){
		int x=U[e[u][i]],y=V[e[u][i]],fx=find(x),fy=find(y);
		if(sz[fx]>sz[fy])fx^=fy^=fx^=fy;
		lst.push_back(fx);fa[fx]=fy;sz[fy]+=sz[fx];
	}if(find(1)==find(n))ans+=v[r]-v[l-1];
	else if(l<r){dfs(u<<1,l,mid);dfs(u<<1|1,mid+1,r);}
	for(int i=lst.size()-1;~i;i--)fa[lst[i]]=lst[i];
	lst.clear();
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d%d",&U[i],&V[i],&L[i],&R[i]);
		v.push_back(L[i]);v.push_back(R[i]+1);
	}sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()),v.end());
	for(int i=1;i<=n;i++){
		fa[i]=i;sz[i]=1;
	}
	for(int i=1;i<=m;i++){
		update(1,1,v.size(),getid(L[i]),getid(R[i]+1)-1,i);
	}dfs(1,1,v.size());printf("%d\n",ans);
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 922ms, 内存消耗: 35648K, 提交时间: 2019-08-13 10:03:34

#include<bits/stdc++.h>
#define lson u<<1,l,mid
#define rson u<<1|1,mid+1,r
using namespace std;
const int N=100005;
int n,m,U[N],V[N],L[N],R[N],fa[N],sz[N],ans;
vector<int>v,e[N<<3];
int find(int x){return fa[x]==x?x:find(fa[x]);}
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
void update(int u,int l,int r,int x,int y,int val){
	if(x<=l&&r<=y){
		e[u].push_back(val);
		return;
	}int mid=(l+r)>>1;
	if(x<=mid)update(lson,x,y,val);
	if(y>mid)update(rson,x,y,val);
}
void dfs(int u,int l,int r){
	vector<int>lst;int mid=(l+r)>>1;
	for(int i=0;i<(int)e[u].size();i++){
		int x=U[e[u][i]],y=V[e[u][i]],fx=find(x),fy=find(y);
		if(sz[fx]>sz[fy])fx^=fy^=fx^=fy;
		lst.push_back(fx);fa[fx]=fy;sz[fy]+=sz[fx];
	}if(find(1)==find(n))ans+=v[r]-v[l-1];
	else if(l<r){dfs(u<<1,l,mid);dfs(u<<1|1,mid+1,r);}
	for(int i=lst.size()-1;~i;i--)fa[lst[i]]=lst[i];
	lst.clear();
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d%d",&U[i],&V[i],&L[i],&R[i]);
		v.push_back(L[i]);v.push_back(R[i]+1);
	}sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()),v.end());
	for(int i=1;i<=n;i++){
		fa[i]=i;sz[i]=1;
	}
	for(int i=1;i<=m;i++){
		update(1,1,v.size(),getid(L[i]),getid(R[i]+1)-1,i);
	}dfs(1,1,v.size());printf("%d\n",ans);
	return 0;
}

上一题