WA #6求调
查看原帖
WA #6求调
716260
tianyu_awa楼主2024/9/17 01:32
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define i128 __int128
#define mem(x, y) memset(x, y, sizeof x)
#define lid (id << 1)
#define rid (id << 1 | 1)
#define fin(x) freopen(#x".in", "r", stdin)
#define fout(x) freopen(#x".out", "w", stdout)
const int N = 1e5+5;
namespace io {
	#define fastio ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
    const int SIZE = (1 << 21) + 1;
    char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
    #ifdef ONLINE_JUDGE
    	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    #else
    	#define gc() getchar()
    #endif
    inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; }
    inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); }
    template <class I> inline void read(I &x){ for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; }
    inline void read(string &s) {s = "";char ch = gc();while (ch == ' ' || ch == '\n' || ch == '\r') ch = gc();while (ch != ' ' && ch != '\n' && ch != '\r') s += ch, ch = gc();}
    inline void read(char &ch) { ch = gc(); while (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) ch = gc(); }
    template <typename T, typename TT> inline void read(pair<T, TT> &a){read(a.first);read(a.second);}
    template <typename T, typename... Args> inline void read(T &num, Args &...args) { read(num), read(args...); }
    template <class I> inline void wr(I x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) putc (qu[qr --]); }
    template <> inline void wr(string ch) {for (char c : ch) putc(c);}
    template <> inline void wr(const char* ch) {wr(string(ch));}
    template <> inline void wr(char ch){ putc(ch); }
    template <typename T, typename TT> inline void wr(pair<T, TT> a, char _sep){wr(a.first);wr(_sep);wr(a.second);}
    inline void wr1(){ ; }
    inline void wr2(){ ; }
    template <typename T, typename... Args> inline void wr1(T num, Args... args) {wr(num), wr(' '), wr1(args...) ;}
    template <typename T, typename... Args> inline void wr2(T num, Args... args) {wr(num), wr('\n'), wr2(args...) ;}
    struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::read;
using io::wr;
using io::wr1;
using io::wr2;
namespace tianyu{
	mt19937 rd(time(0));
	#define pii pair<int, int> 
	int n, k;
	int dep[N];
	int fa[N];
	priority_queue<pii> pq;
	vector<int> G[N];
	int center;
	int mxdep[N];
	int d1[N], d2[N];
	int zj = 0;
	void dfs(int u, int f){
		d1[u] = 0;
		d2[u] = 0;
		for (int v : G[u]){
			if (v == f) continue;
			dfs(v, u);
			int tmp = d1[v] + 1;
			if (tmp > d1[u]) d2[u] = d1[u], d1[u] = tmp;
			else if (tmp > d2[u]) d2[u] = tmp;
		}
		if (d1[u] + d2[u] > zj) zj = d1[u] + d2[u];
		if (d1[u] + d2[u] == zj && d1[u] - d2[u] <= 1) center = u;
	}
	void dfs1(int u, int f){
		dep[u] = dep[f] + 1;
		mxdep[u] = dep[u];
		for (int v : G[u]){
			if (v == f) continue;
			fa[v] = u;
			dfs1(v, u);
			mxdep[u] = max(mxdep[u], mxdep[v]);
		}
	}
	bool vis[N];
	int ans;
	void dfs3(int u, int f, int dep1){
		if (dep1) ans = max(ans, dep[u] - dep1);
		for (int v : G[u]){
			if (v == f) continue;
			if (dep1) dfs3(v, u, dep1);
			else if (!vis[v]) dfs3(v, u, dep[u]);
			else dfs3(v, u, 0);
		}
	}
	void awa(){
		read(n, k);
		for (int i = 1;i < n;i++){
			int u, v;
			read(u, v);
			G[u].emplace_back(v);
			G[v].emplace_back(u);
		}
		dfs(1, 0);
		dfs1(center, 0);
		for (int v : G[center]) pq.push({mxdep[v] - dep[v] + 1, v});
		vis[center] = 1;
		--k;
		while (k--){
			pii hd = pq.top();
			pq.pop();
			vis[hd.second] = 1;
			for (int v : G[hd.second]){
				if (v == fa[hd.second]) continue;
				pq.push({mxdep[v] - dep[v] + 1, v});
			}
		}
		for (int i = 1;i <= n;i++) if (!vis[i]) ans = max(ans, mxdep[i] - dep[i] + 1);
		wr(ans);
	}
}
_Atomic_word main(){ 
	fastio;
	int T = 1;
	while (T--){
		tianyu::awa();
	}
	return EXIT_SUCCESS;
}
2024/9/17 01:32
加载中...