Description
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。Input
输入文件第一行为3个整数:N, M, Q分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。
以下M行,每行3个整数x, y和t,描述一条对应的水管。x和y表示水管两端结点的编号,t表示准备送水所需要的时间。我们不妨为结点从1至N编号,这样所有的x和y都在范围[1, N]内。
以下Q行,每行描述一项任务。其中第一个整数为k:若k=1则后跟两个整数A和B,表示你需要为供水公司寻找一条满足要求的从A到B的水管路径;若k=2,则后跟两个整数x和y,表示直接连接x和y的水管宣布报废(保证合法,即在此之前直接连接x和y尚未报废的水管一定存在)。Output
按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。Sample Input
4 4 3
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4Sample Output
2
3【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【C/C++选手注意事项】
由于此题输入规模较大(最大的测试点约20MB),因此即使使用scanf读入数据也会花费较多的时间。为了节省读入耗时,建议使用以下函数读入正整数(返回值为输入文件中下一个正整数):
int getint()
{
char ch = getchar();
for ( ; ch > ‘9’ || ch < ‘0’; ch = getchar());
int tmp = 0;
for ( ; ‘0’ <= ch && ch <= ‘9’; ch = getchar())
tmp = tmp * 10 + int(ch) - 48;
return tmp;
}
这个可以搞成一个大概最小生成树的样子.. 从后往前操作,然后用LCT维护这棵树就好了..
这道题敲到我手抽筋..
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 1100010;
int maxx[Maxn], fa[Maxn], c[Maxn][2], val[Maxn], num[Maxn], rev[Maxn];
int n, m, q; bool v[Maxn];
struct node {
int x, y, t, num;
}list[Maxn], b[Maxn]; int tot;
int read (){
char ch = getchar ();
for ( ; ch > '9' || ch < '0'; ch = getchar () );
int tmp = 0;
for ( ; ch >= '0' && ch <= '9'; ch = getchar () ) tmp = tmp*10+ch-'0';
return tmp;
}
struct qnode {
int x, y, fl, num;
}Q[Maxn]; int ans[Maxn], nq;
bool cmp1 ( node x, node y ){
if ( x.x != y.x ) return x.x < y.x;
else return x.y < y.y;
}
bool cmp2 ( node x, node y ){
if ( x.t != y.t ) return x.t < y.t;
if ( x.x != y.x ) return x.x < y.x;
return x.y < y.y;
}
int st[Maxn], tp;
int _max ( int x, int y ){ return x > y ? x : y; }
bool is_root ( int x ){ return c[fa[x]][0] != x && c[fa[x]][1] != x; }
void push_down ( int x ){
if ( rev[x] ){
rev[x] = 0;
int lc = c[x][0], rc = c[x][1];
swap ( c[x][0], c[x][1] );
rev[lc] ^= 1; rev[rc] ^= 1;
}
}
void update ( int x ){
int lc = c[x][0], rc = c[x][1];
maxx[x] = _max ( val[x], _max ( maxx[lc], maxx[rc] ) );
if ( maxx[x] == val[x] ) num[x] = x;
else if ( maxx[x] == maxx[lc] ) num[x] = num[lc];
else num[x] = num[rc];
}
void prep ( int x ){
tp = 0; int i;
for ( i = x; !is_root (i); i = fa[i] ) st[++tp] = i;
st[++tp] = i;
for ( i = tp; i >= 1; i -- ) push_down (st[i]);
}
void rotate ( int x ){
int y = fa[x], z = fa[y], l, r;
if ( c[y][0] == x ) l = 0; else l = 1; r = l^1;
if ( !is_root (y) ){ if ( c[z][0] == y ) c[z][0] = x; else c[z][1] = x; }
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
update (y); update (x);
}
void splay ( int x ){
prep (x);
while ( !is_root (x) ){
int y = fa[x], z = fa[y];
if ( !is_root (y) ){
if ( (c[y][0]==x)^(c[z][0]==y) ) rotate (x);
rotate (y);
}
rotate (x);
}
}
void access ( int x ){
int t = 0;
while (x){
splay (x);
c[x][1] = t;
update (x);
t = x;
x = fa[x];
}
}
int find_root ( int x ){
access (x); splay (x);
while ( c[x][0] ) x = c[x][0];
return x;
}
void make_root ( int x ){ access (x); splay (x); rev[x] ^= 1; }
void link ( int x, int y ){ make_root (x); fa[x] = y; }
void cut ( int x, int y ){ make_root (x); access (y); splay (y); fa[x] = 0; c[y][0] = 0; }
bool connect ( int x, int y ){ return find_root (x) == find_root (y); }
void get_max ( int x, int y ){
make_root (x);
access (y); splay (y);
}
int main (){
int i, j, k;
n = read (); m = read (); q = read ();
for ( i = 1; i <= m; i ++ ){
list[i].x = read (); list[i].y = read (); list[i].t = read ();
if ( list[i].x > list[i].y ) swap ( list[i].x, list[i].y );
}
tot = 0; nq = 0;
for ( i = 1; i <= q; i ++ ){
Q[i].fl = read (); Q[i].x = read (); Q[i].y = read ();
if ( Q[i].x > Q[i].y ) swap ( Q[i].x, Q[i].y );
if ( Q[i].fl == 2 ){
tot ++;
b[tot].x = Q[i].x; b[tot].y = Q[i].y; b[tot].num = i;
}
else Q[i].num = ++nq;
}
sort ( list+1, list+m+1, cmp2 );
for ( i = 1; i <= m; i ++ ){
list[i].num = i;
val[i+n] = list[i].t;
}
sort ( b+1, b+tot+1, cmp1 );
sort ( list+1, list+m+1, cmp1 );
i = 1;
memset ( v, true, sizeof (v) );
for ( j = 1; j <= tot; j ++ ){
while ( b[j].x != list[i].x || b[j].y != list[i].y ) i ++;
Q[b[j].num].num = list[i].num;
v[list[i].num] = false;
}
sort ( list+1, list+m+1, cmp2 );
int sum = 0;
for ( i = 1; i <= m; i ++ ){
if ( !connect ( list[i].x, list[i].y ) && v[list[i].num] == true ){
link ( list[i].x, i+n );
link ( list[i].y, i+n );
sum ++;
}
if ( sum == n-1 ) break;
}
for ( i = q; i >= 1; i -- ){
if ( Q[i].fl == 1 ){
get_max ( Q[i].x, Q[i].y );
ans[Q[i].num] = maxx[Q[i].y];
}
else {
get_max ( Q[i].x, Q[i].y );
int p = num[Q[i].y];
if ( maxx[Q[i].y] > list[Q[i].num].t ){
cut ( list[p-n].x, p );
cut ( list[p-n].y, p );
link ( Q[i].x, Q[i].num+n );
link ( Q[i].y, Q[i].num+n );
}
}
}
for ( i = 1; i <= nq; i ++ ) printf ( "%d\n", ans[i] );
return 0;
}