[NOI2014]魔法森林

嘟嘟嘟


lct维护生成树。


其实要是没学lct的话,我觉得是二维spfa,但是没写不知道对不对。
用lct的话,就是先把这些遍按\(a\)排序,这样我们只用考虑另一维了。
对于边\(<x, y>\),如果\(x\)\(y\)不连通,直接Link即可;如果联通,那么就要从\(x\)\(y\)的这条链中Cut一个边权最大的边,再把这条边连上。
所以我们还要维护链上最大值。而且记录的是最大值所在的节点编号。


但是lct不好维护边的信息。因此我们把边看成点,比如对于一条边权为\(2\)的边\(<x, y>\),应该\(x - e - y\)这么连,其中\(e\)的权值为2。
所以Link和Cut的写法可能会稍有不同。
Link时,要先把\(x\)变成根节点,然后用虚边连接\(x - e - y\)
Cut时,相当于提取\(x - e - y\)这条链,然后把\(e\)splay到根节点,这时候\(e\)的左右儿子就是\(x\)\(y\)了,断掉即可。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxm = 1e5 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, m;
struct Edge
{
  int u, v, a, b;
  bool operator < (const Edge& oth)const
  {
    return a < oth.a || (a == oth.a && b < oth.b);
  }
}e[maxm << 1];

struct Tree
{
  int ch[2], fa;
  int pos, rev;
}t[maxm << 1];
In bool n_root(int now)
{
  return t[t[now].fa].ch[0] == now || t[t[now].fa].ch[1] == now;
}
In void change(int now)
{
  swap(t[now].ch[0], t[now].ch[1]);
  t[now].rev ^= 1;
}
In void pushdown(int now)
{
  if(t[now].rev)
    {
      if(t[now].ch[0]) change(t[now].ch[0]);
      if(t[now].ch[1]) change(t[now].ch[1]);
      t[now].rev = 0;
    }
}
In void pushup(int now)
{
  t[now].pos = now;
  if(e[t[now].pos].b < e[t[t[now].ch[0]].pos].b) t[now].pos = t[t[now].ch[0]].pos;
  if(e[t[now].pos].b < e[t[t[now].ch[1]].pos].b) t[now].pos = t[t[now].ch[1]].pos;
}
In void rotate(int x)
{
  int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
  if(n_root(y)) t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
  t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
  t[x].ch[k ^ 1] = y; t[y].fa = x;
  pushup(y), pushup(x);
}
int st[maxm << 1], top = 0;
In void splay(int x)
{
  int y = x;
  st[top = 1] = y;
  while(n_root(y)) st[++top] = y = t[y].fa;
  while(top) pushdown(st[top--]);
  while(n_root(x))
    {
      int y = t[x].fa, z = t[y].fa;
      if(n_root(y)) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
      rotate(x);
    }
}
In void access(int x)
{
  int y = 0;
  while(x)
    {
      splay(x); t[x].ch[1] = y;
      pushup(x);
      y = x; x = t[x].fa;
    }
}
In void make_root(int x)
{
  access(x); splay(x);
  change(x);
}
In int find_root(int x)
{
  access(x); splay(x);
  while(t[x].ch[0]) pushdown(x), x = t[x].ch[0];
  return x;
}
In void Link(int x)
{
  make_root(e[x].v);
  t[t[e[x].v].fa = x].fa = e[x].u;
}
In void Cut(int x)
{
  make_root(e[x].v); access(e[x].u); splay(x);
  t[x].ch[0] = t[x].ch[1] = t[t[x].ch[0]].fa = t[t[x].ch[1]].fa = 0;
  pushup(x);
}

int main()
{
  n = read(); m = read();
  for(int i = 1; i <= m; ++i) 
    e[i].u = read() + m, e[i].v = read() + m, e[i].a = read(), e[i].b = read();
  sort(e + 1, e + m + 1);
  int ans = INF;
  for(int i = 1, x, y; i <= m; ++i)
    {
      if((x = e[i].u) == (y = e[i].v)) continue;
      make_root(x);
      if(find_root(y) != x) Link(i);
      else if(e[i].b < e[t[y].pos].b) Cut(t[y].pos), Link(i);
      make_root(1 + m);
      if(find_root(n + m) == 1 + m) ans = min(ans, e[i].a + e[t[n + m].pos].b);
    }
  write(ans == INF ? -1 : ans), enter;
  return 0;
}
posted @ 2018-12-21 17:25  mrclr  阅读(160)  评论(0编辑  收藏  举报