题目传送门:点击打开链接
Andrew plays a game called "Civilization". Dima helps him.
The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct cities v1, v2, ..., vk, that there is a road between any contiguous cities vi and vi + 1 (1 ≤ i < k). The length of the described path equals to (k - 1). We assume that two cities lie in the same region if and only if, there is a path connecting these two cities.
During the game events of two types take place:
- Andrew asks Dima about the length of the longest path in the region where city x lies.
- Andrew asks Dima to merge the region where city x lies with the region where city y lies. If the cities lie in the same region, then no merging is needed. Otherwise, you need to merge the regions as follows: choose a city from the first region, a city from the second region and connect them by a road so as to minimize the length of the longest path in the resulting region. If there are multiple ways to do so, you are allowed to choose any of them.
Dima finds it hard to execute Andrew's queries, so he asks you to help him. Help Dima.
The first line contains three integers n, m, q (1 ≤ n ≤ 3·105; 0 ≤ m < n; 1 ≤ q ≤ 3·105) — the number of cities, the number of the roads we already have and the number of queries, correspondingly.
Each of the following m lines contains two integers, ai and bi (ai ≠ bi; 1 ≤ ai, bi ≤ n). These numbers represent the road between cities aiand bi. There can be at most one road between two cities.
Each of the following q lines contains one of the two events in the following format:
- 1 xi. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city xi (1 ≤ xi ≤ n).
- 2 xi yi. It is the request Andrew gives to Dima to merge the region that contains city xi and the region that contains city yi(1 ≤ xi, yi ≤ n). Note, that xi can be equal to yi.
For each event of the first type print the answer on a separate line.
6 0 6 2 1 2 2 3 4 2 5 6 2 3 2 2 5 3 1 1
4
题解:并查集+dfs(一开始我用的BFS超时了..现在还没找到原因)
首先求出每棵树及其直径,然后对于合并操作,转移方程为:
f[x]=y;
s[y] = max( (s[x]+1)/2+(s[y]+1)/2+1 , max( s[x] , s[y] ) );
DFS代码 :AC
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
const int maxn=300005;
vector<int>v[maxn];
int lon=-1;
int d;
int f[maxn];
int s[maxn];
int find(int x)
{
if (f[x]==x) return x;
return f[x]=find(f[x]);
}
void dfs(int x,int t,int fa)
{
if (t>lon)
{
lon=t;
d=x;
}
for (int i=0;i<v[x].size();i++)
{
if (v[x][i]!=fa) dfs(v[x][i],t+1,x);
}
}
int main()
{
int n,m,k;
int l,r;
int x,y,num;
while (~scanf("%d%d%d",&n,&m,&k))
{
for (int i=1;i<=n;i++) f[i]=i;
for (int i=1;i<=n;i++) v[i].clear();
for (int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
x=find(l);
y=find(r);
if (x!=y) f[x]=y;
v[l].push_back(r);
v[r].push_back(l);
}
for (int i=1;i<=n;i++)
{
if (find(i)==i)
{
lon=-1;
dfs(i,0,0);
lon=-1;
dfs(d,0,0);
s[i]=lon;
}
}
for (int i=1;i<=k;i++)
{
scanf("%d",&num);
if (num==1)
{
scanf("%d",&x);
printf("%d\n",s[find(x)]);
}
else
{
scanf("%d%d",&l,&r);
x=find(l);
y=find(r);
if (x!=y)
{
f[x]=y;
s[y]=max((s[y]+1)/2+(s[x]+1)/2+1,max(s[x],s[y]));
}
}
}
}
return 0;
}
BFS代码 :TLE.所以说CF的数据的时间是累计的而不是单独计的?前面30W个点的数据都过了在10W个点的时候TLE了。
或者是有什么奇怪的地方写错了,请指教。
还有..虽然函数名字写的是DFS...那只是个人习惯...实际上是BFS
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
const int maxn=300005;
vector<int>v[maxn];
int f[maxn];
int s[maxn];
int check[maxn];
int find(int x)
{
if (f[x]==x) return x;
return f[x]=find(f[x]);
}
struct point
{
int n;
int t;
point (int _n=0,int _t=0)
{
n=_n;
t=_t;
}
};
queue<point>q;
int dfs(int x,int flag)
{
point tem;
check[x]=flag;
while (!q.empty()) q.pop();
q.push(point(x,0));
while (!q.empty())
{
tem=q.front();
q.pop();
for (int i=0;i<v[tem.n].size();i++)
{
if (check[v[tem.n][i]]!=flag)
{
q.push(point(v[tem.n][i],tem.t+1));
check[v[tem.n][i]]=flag;
}
}
}
int num=find(tem.n);
s[num]=tem.t;
return tem.n;
}
int main()
{
int n,m,k;
int l,r;
int x,y,num;
while (~scanf("%d%d%d",&n,&m,&k))
{
for (int i=1;i<=n;i++) f[i]=i;
for (int i=1;i<=n;i++) v[i].clear();
for (int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
x=find(l);
y=find(r);
if (x!=y) f[x]=y;
v[l].push_back(r);
v[r].push_back(l);
}
memset(check,0,sizeof(check));
for (int i=1;i<=n;i++)
{
if (find(i)!=i)
{
x=dfs(i,1);
y=dfs(x,2);
}
}
for (int i=1;i<=k;i++)
{
scanf("%d",&num);
if (num==1)
{
scanf("%d",&x);
printf("%d\n",s[find(x)]);
}
else
{
scanf("%d%d",&l,&r);
x=find(l);
y=find(r);
if (x!=y)
{
f[x]=y;
s[y]=max((s[y]+1)/2+(s[x]+1)/2+1,max(s[x],s[y]));
}
}
}
}
return 0;
}