WA #6求调
查看原帖
WA #6求调
716260
tianyu_awa楼主2024/9/17 02:12
#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 fa[N];
	vector<int> G[N];
	int dis[N];
	int dep[N], mxdep[N];
	void dfs(int u, int f){
		dis[u] = dis[f] + 1;
		for (int v : G[u]){
			if (v == f) continue;
			fa[v] = u;
			dfs(v, u);
		}
	}
	void dfs1(int u, int f){
		mxdep[u] = dep[u];
		for (int v : G[u]){
			if (v == f) continue;
			fa[v] = u;
			dep[v] = dep[u] + 1;
			dfs1(v, u);
			mxdep[u] = max(mxdep[u], mxdep[v]);
		}
	}
	bool vis[N];
	int ans;
	int abc[N];
	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);
		int qwq = 0, awa = 1;
		for (int i = 1;i <= n;i++){
			if (dis[i] > qwq){
				qwq = dis[i];
				awa = i;
			}
		}
		dfs(awa, 0);
		qwq = 0, awa = 1;
		for (int i = 1;i <= n;i++){
			if (dis[i] > qwq){
				qwq = dis[i];
				awa = i;
			}
		}
		
		for (int i = 1;i <= dis[awa] / 2;i++) awa = fa[awa];
		dep[awa] = 1;
		dfs1(awa, 0);
		for (int i = 1;i <= n;i++) abc[i] = i;
		sort(abc + 1, abc + 1 + n, [](int a, int b){
			return (mxdep[a] - dep[a]) > (mxdep[b] - dep[b]);
		});
		for (int i = 1;i <= k;i++) vis[abc[i]] = 1;
		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 02:12
加载中...