QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#809213#7216. Boxes on treeciuimWA 2ms10396kbC++145.6kb2024-12-11 13:17:582024-12-11 13:18:00

Judging History

你现在查看的是最新测评结果

  • [2024-12-11 13:18:00]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:10396kb
  • [2024-12-11 13:17:58]
  • 提交

answer

bool M1;
#define look_memory cerr<<abs(&M2-&M1)/1024.0/1024<<" MB\n"
#define look_time cerr<<(clock()-Time)*1.0/CLOCKS_PER_SEC<<'\n'
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <cstring>
#include <array>
#include <algorithm>
#include <queue>
#include <vector>
#include <bitset>
#include <ctime>
#include <cstdlib>
#include <random>
#include <set>
#include <ctime>
#include <map>
#include <stack>
#include <unordered_map>
#include <assert.h>
#include <unordered_set>
#define i128 __int128
#define ll long long
#define uint unsigned
#define ull unsigned long long
#define ld long double
#define fo(a,b,c) for(ll a=b;a<=c;++a)
#define re(a,b,c) for(ll a=b;a>=c;--a)
#define pii pair<ll,ll>
#define pdd pair<db,db>
#define fi first
#define pb push_back
#define se second
#define ite set<array<ll,3>> ::iterator
#define vite vector<ll> ::iterator
#define mite map<ll,ll> ::iterator
using namespace std;
const ll mod=1e9+7;
inline ll gi()
{
	ll x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = (x<<1) + (x<<3) + (ch^48);
		ch = getchar();
	}
	return x * f;
}
ll _=1;
const ll inf=2e17+5,iinf=1e9+5;
const ll N=5005,M=5000005;
struct E
{
	ll u,v,w;
};
vector<E> e;
vector<ll> g[N];
ll val[N],p[N],n,f[N],tag[N],m,X[M],Y[M],V[M],dep[N],mst[N];
void dfs(ll u,ll fa)
{
	dep[u]=dep[fa]+1;
	if(p[u]==u) tag[u]=1;
	else tag[u]=0;
	f[u]=fa;
	for(ll v:g[u])
	{
		if(v==fa) continue;
		dfs(v,u);
		if(tag[v]==0) tag[u]=0;
	}
}
			namespace WW
			{
			typedef long long LL;
			using namespace std;
			template<typename T> inline void chkmin(T &a, const T &b) { a = a < b ? a : b; }
			template<typename T> inline void chkmax(T &a, const T &b) { a = a > b ? a : b; }
			
			const int MAXN = M;
			struct Node {
				int v, w, h, tag, ls, rs;
			} nd[MAXN];
			int rt, pq[N], tpar[N], spar[N], nxt[N], val[N];
			
			int find(int *par, int x) {
				return x == par[x] ? x : par[x] = find(par, par[x]);
			}
			
			inline void push_down(int k) {
				if (!nd[k].tag) return;
				int ls = nd[k].ls, rs = nd[k].rs, &t = nd[k].tag;
				nd[ls].w += t, nd[ls].tag += t;
				nd[rs].w += t, nd[rs].tag += t;
				t = 0;
			}
			
			int merge(int x, int y) {
				if (!x || !y) return x + y;
				push_down(x);
				push_down(y);
				if (nd[x].w > nd[y].w) swap(x, y);
				nd[x].rs = merge(nd[x].rs, y);
				if (nd[nd[x].ls].h < nd[nd[x].rs].h) swap(nd[x].ls, nd[x].rs);
				nd[x].h = nd[nd[x].rs].h + 1;
				return x;
			}
			
			inline void to_zero(int x) {
				nd[x].tag -= nd[x].w;
				nd[x].w = 0;
			}
			
			inline void pop(int &x) {
				push_down(x);
				x = merge(nd[x].ls, nd[x].rs);
			}
			
			struct Data {
				int u, w;
				bool operator==(const Data &d) const { return u == d.u && w == d.w; }
				bool operator<(const Data &d) const { return w == d.w ? u > d.u : w > d.w; }
			};
			struct Set {
				priority_queue<Data> del, ins;
				void push(const Data &d) { ins.push(d); }
				void erase(const Data &d) { del.push(d); }
				void upd() { while (!del.empty() && del.top() == ins.top()) del.pop(), ins.pop(); }
				bool empty() { upd(); return ins.empty(); }
				Data top() { upd(); return ins.top(); }
				void pop() { upd(); ins.pop(); }
			} ss,ts;
			
			int mian() {
				ss=ts;
				memset(pq,0,sizeof(pq));
				memset(tpar,0,sizeof(tpar));
				memset(nxt,0,sizeof(nxt));
				memset(val,0,sizeof(val));
				rt=1;
				for (int i = 1; i <= m; i++) {
					int u=X[i], v=Y[i], w=V[i];
					if (u == v || v == rt) continue;
					nd[i] = Node { u, w, 1, 0, 0, 0 };
					pq[v] = merge(pq[v], i);
				}
				for (int i = 1; i <= n; i++) spar[i] = tpar[i] = i;
				for (int i = 1; i <= n; i++) if (i != rt) {
					if (!pq[i]) return puts("-1"), 0;
					ss.push(Data { i, val[i] = nd[pq[i]].w });
				}
				int ans = 0;
				for (int i = 1; i < n; i++) {
					if (ss.empty()) return puts("-1"), 0;
					int u = ss.top().u, w = ss.top().w;
					ans += w, ss.pop();
					to_zero(pq[u]);
					tpar[u] = find(tpar, nxt[u] = nd[pq[u]].v);
					pop(pq[u]);
					u = tpar[u];
					
					if (pq[u]) {
						ss.erase(Data { u, nd[pq[u]].w });
						w = 0;
						while (find(tpar, nd[pq[u]].v) == u) {
							int v = find(spar, nd[pq[u]].v);
							w += nd[pq[u]].w;
							to_zero(pq[u]);
							pop(pq[u]);
							while (v != u) {
								pq[u] = merge(pq[u], pq[v]);
								spar[v] = u;
								v = find(spar, nxt[v]);
							}
						}
						if (pq[u]) {
							nd[pq[u]].w += w, nd[pq[u]].tag += w;
							ss.push(Data { u, nd[pq[u]].w });
						}
					}
				}
				return ans;
			}
			}
void sol()
{
	e.clear();
	n=gi();
	m=0;
	fo(i,1,n) g[i].clear(),mst[i]=0;
	fo(i,1,n) p[i]=gi(),val[i]=0;
	fo(i,1,n-1)
	{
		ll x=gi(),y=gi();
		g[x].pb(y);
		g[y].pb(x);
	}
	dfs(1,0);
	fo(i,1,n)
	{
		ll x=i,y=p[i];
		e.pb({x,y,0});
		e.pb({y,x,0});
		while(x!=y)
		{
			if(dep[x]<dep[y]) swap(x,y);
			val[x]++;
			x=f[x];
			e.pb({x,i,0});
			if(mst[x]==0)
			{
				mst[x]=i;
			}
			else if(dep[i]<dep[mst[x]]) mst[x]=i;
		}
	}
//	fo(x,1,n)
//	{
//		if(mst[x]) e.pb({x,mst[x],0});
//	}
	ll ans=0;
	fo(i,1,n)
	{
		ans+=val[i];
		if(f[i])
		{
			e.pb({i,f[i],2});
		}
	}
	for(E z:e)
	{
		m++;
		X[m]=z.v,Y[m]=z.u,V[m]=z.w;
	}
	fo(i,1,n) if(tag[i]) ans-=2;
	ans+=WW::mian();
	cout<<ans;
}
bool M2;
int main()
{
//	freopen("classic.in","r",stdin);
//	freopen("classic.out","w",stdout);
	int Time=clock();
	look_memory;
//	_=gi();
	while(_--)
	{
		sol();
		printf("\n");
	}
	look_time;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10256kb

input:

3
1 3 2
1 2
2 3

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 2ms
memory: 10396kb

input:

4
2 1 4 3
1 3
3 2
3 4

output:

6

result:

ok 1 number(s): "6"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 10388kb

input:

1
1

output:

-2

result:

wrong answer 1st numbers differ - expected: '0', found: '-2'