1. 程式人生 > 實用技巧 >Solution -「CF 1375G」Tree Modification

Solution -「CF 1375G」Tree Modification

\(\mathcal{Description}\)

  Link.

  給定一棵 \(n\) 個結點的樹,每次操作選擇三個結點 \(a,b,c\),滿足 \((a,b),(b,c)\in E\),並令 \(a\) 的所有鄰接點(包括 \(b\))與 \(c\) 鄰接且不再與 \(a\) 鄰接;再令 \(a\)\(c\) 鄰接。求至少幾次操作使樹變為菊花圖。

  \(n\le2\times10^5\)

  操作圖例:

\(\mathcal{Solution}\)

  和 CF1025G 有點類似。不妨令 \(1\) 為樹的根,結點 \(u\) 的深度記為 \(d(u)\)\(d(1)=1\)

。構造勢能函式 \(\Phi:T\rightarrow\mathbb N_+\),有:

\[\Phi(T)=\sum_{u\in T}[2|d(u)] \]

  先考慮目標狀態,菊花圖的勢能顯然為 \(1\)(根是花瓣)或 \(n-1\)(根是花蕊)。再觀察一次操作帶來的勢能變化,發現僅有 \(a\) 結點的深度的奇偶性改變,那麼:

\[\Delta\Phi=\pm1 \]

  記初始時樹為 \(S\),可知答案為:

\[\min\{(n-1)-\Phi(S),\Phi(S)-1\} \]

  複雜度 \(\mathcal O(n)\)。嗯唔,做完了 www!

\(\mathcal{Code}\)

/* Clearink */

#include <cstdio>

inline int rint () {
	int x = 0, f = 1; char s = getchar ();
	for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
	for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
	return x * f;
}

template<typename Tp>
inline void wint ( Tp x ) {
	if ( x < 0 ) putchar ( '-' ), x = ~ x + 1;
	if ( 9 < x ) wint ( x / 10 );
	putchar ( x % 10 ^ '0' );
}

const int MAXN = 2e5;
int n, ecnt, head[MAXN + 5], cnt[2];

struct Edge { int to, nxt; } graph[MAXN * 2 + 5];

inline void link ( const int s, const int t ) {
	graph[++ ecnt] = { t, head[s] };
	head[s] = ecnt;
}

inline void solve ( const int u, const int f, const int dep ) {
	++ cnt[dep & 1];
	for ( int i = head[u], v; i; i = graph[i].nxt ) {
		if ( ( v = graph[i].to ) ^ f ) {
			solve ( v, u, dep + 1 );
		}
	}
}

int main () {
	n = rint ();
	for ( int i = 1, u, v; i < n; ++ i ) {
		u = rint (), v = rint ();
		link ( u, v ), link ( v, u );
	}
	solve ( 1, 0, 0 );
	printf ( "%d\n", ( cnt[0] < cnt[1] ? cnt[0] : cnt[1] ) - 1 );
	return 0;
}

\(\mathcal{Details}\)

  勢能分析的方法有點像數學上的特徵值法。這種操作題沒思路的時候不妨研究一下單次操作,構造出一個變化極為簡單的“特徵”來快速求解。