一个玄学的问题,急,在线等
  • 板块学术版
  • 楼主Luckies
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/6/17 15:07
  • 上次更新2023/10/23 12:57:11
查看原帖
一个玄学的问题,急,在线等
520338
Luckies楼主2023/6/17 15:07

rt,P3304的玄学错误,和同学的代码做法几乎一样,不过同学的代码不卡常也跑得飞快,我的代码卡了常也T掉。

急,在线等。

My Code

#include<bits/stdc++.h>
#define int long long
#define cin qin
#define cout qout
using namespace std;
namespace FastIO {
    class FastIOBase {
        protected:
#ifdef OPENIOBUF
            static const int BUFSIZE = 1 << 22;
            char buf[BUFSIZE + 1];
            int buf_p = 0;
#endif
            FILE*target;
        public:
#ifdef OPENIOBUF
            virtual void flush() = 0;
#endif
            FastIOBase(FILE*f): target(f) {}~FastIOBase() = default;
    };
    class FastOutput: public FastIOBase {
#ifdef OPENIOBUF
        public:
            inline void flush() {
                fwrite(buf, 1, buf_p, target), buf_p = 0;
            }
#endif
        protected:
            inline void __putc(char x) {
#ifdef OPENIOBUF
                if (buf[buf_p++] = x, buf_p == BUFSIZE)flush();
#else
                putc(x, target);
#endif
            } template<typename T>inline void __write(T x) {
                static char stk[64], *top;
                top = stk;
                if (x < 0)return __putc('-'), __write(-x);
                do*(top++) = x % 10, x /= 10;
                while (x);
                for (; top != stk; __putc(*(--top) + '0'));
            } public:
            FastOutput(FILE*f = stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
            inline void setTarget(FILE*f) {
                this->flush(), target = f;
            }~FastOutput() {
                flush();
            }
#else
            inline void setTarget(FILE*f) {
                target = f;
            }
#endif
            template<typename...T>inline void writesp(const T&...x) {
                initializer_list<int> {(this->operator<<(x), __putc(' '), 0)...};
            } template<typename...T>inline void writeln(const T&...x) {
                initializer_list<int> {(this->operator<<(x), __putc('\n'), 0)...};
            } inline FastOutput&operator<<(char x) {
                return __putc(x), *this;
            } inline FastOutput&operator<<(const char*s) {
                for (; *s; __putc(*(s++)));
                return*this;
            } inline FastOutput&operator<<(const string&s) {
                return (*this) << s.c_str();
            } template<typename T, typename = typename enable_if<is_integral<T>::value>::type>inline FastOutput & operator<<(const T&x) {
                return __write(x), *this;
            }
    } qout;
    class FastInput: public FastIOBase {
#ifdef OPENIOBUF
        public:
            inline void flush() {
                buf[fread(buf, 1, BUFSIZE, target)] = '\0', buf_p = 0;
            }
#endif
        protected:
            inline char __getc() {
#ifdef OPENIOBUF
                if (buf_p == BUFSIZE)flush();
                return buf[buf_p++];
#else
                return getc(target);
#endif
            } public:
#ifdef OPENIOBUF
            FastInput(FILE*f = stdin): FastIOBase(f) {
                buf_p = BUFSIZE;
            } inline void setTarget(FILE*f) {
                this->flush(), target = f;
            }
#else
            FastInput(FILE*f = stdin): FastIOBase(f) {} inline void setTarget(FILE*f) {
                target = f;
            }
#endif
            inline char getchar() {
                return __getc();
            } template<typename...T>inline void read(T&...x) {
                initializer_list<int> {(this->operator>>(x), 0)...};
            } inline FastInput&operator>>(char&x) {
                while (isspace(x = __getc()));
                return*this;
            } template<typename T, typename = typename enable_if<is_integral<T>::value>::type>inline FastInput & operator>>(T&x) {
                static char ch, sym;
                x = sym = 0;
                while (isspace(ch = __getc()));
                if (ch == '-')sym = 1, ch = __getc();
                for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = __getc());
                return sym ? x = -x : x, *this;
            } inline FastInput&operator>>(char*s) {
                while (isspace(*s = __getc()));
                for (; !isspace(*s) && *s && ~*s; * (++s) = __getc());
                return*s = '\0', *this;
            } inline FastInput&operator>>(string&s) {
                char str_buf[(1 << 8) + 1], *p = str_buf;
                char*const buf_end = str_buf + (1 << 8);
                while (isspace(*p = __getc()));
                for (s.clear(), p++;; p = str_buf) {
                    for (; p != buf_end && !isspace(*p = __getc()) && *p && ~*p; p++);
                    *p = '\0', s.append(str_buf);
                    if (p != buf_end)break;
                }
                return*this;
            }
    } qin;
} using namespace FastIO;
const int N = 2e5 + 5;
int n, dis1[N], dis2[N], ld[N], rd[N], lt, rt, maxi = -1e9, ans, fa[N], son[N];
struct node
{
	int y, w;
};
bool vis[N];
vector<node> e[N];
void dfs1(int x, int f)
{
	for(auto i : e[x])
	{
		int y = i.y, w = i.w;
		if(y == f)
			continue;
		fa[y] = x;
		dis1[y] = dis1[x] + w;
		dfs1(y, x);
	}
	return;
}
void dfs2(int x)
{
	for(auto i : e[x])
	{
		int y = i.y, w = i.w;
		if(fa[x] == y || vis[y])
			continue;
		dfs2(y);
		dis2[x] = max(dis2[x], dis2[y] + w);
	}
	return;
}
signed main()
{
	cin >> n;
	for(int i = 1; i < n; i++)
	{
		int x, y, w;
		cin >> x >> y >> w;
		e[x].push_back({y, w});
		e[y].push_back({x, w});
	}
	dfs1(1, 0);
	for(int i = 1; i <= n; i++)
		if(dis1[i] > maxi)
			maxi = dis1[i], lt = i, dis1[i] = 0;
	dfs1(lt, 0);
	for(int i = 1; i <= n; i++)
		if(dis1[i] > maxi)
			maxi = dis1[i], rt = i;
	cout << maxi << "\n";
	int x = rt;
	while(x != lt)
	{
		vis[x] = true;
		son[fa[x]] = x;
		x = fa[x];
	}
	x = rt;
	while(x != lt)
	{
		dfs2(x);
		if(dis2[x] == maxi - dis1[x])
			rt = x;
		x = fa[x];
	}
	x = lt;
	while(x != rt)
	{
		dfs2(x);
		if(dis2[x] == dis1[x])
			lt = x;
		x = son[x];
	}
	int ans = 0;
	while(lt != rt)
	{
		lt = son[lt];
		ans++;
	}
	cout << ans;
	return 0;
}

同学's Code

#include<bits/stdc++.h>
using namespace std;
#define int long long

int n,x,y,z,head[200010],cnt,ans,r,s,t,l,fa[200010],son[200010],diss[200010],dis[200010],ANS;
bool vis[200010];
struct edge{
	int next,to,dist;
}e[400010];
void add(int u,int v,int w){
	e[++cnt].to=v;
	e[cnt].dist=w;
	e[cnt].next=head[u];
	head[u]=cnt;
}
void dfs(int cur,int root){
	for(int i=head[cur];i;i=e[i].next){
		int y=e[i].to;
		if(y==root)continue;
		fa[y]=cur;
		dis[y]=dis[cur]+e[i].dist;
		dfs(y,cur);
	}
}
void dfs2(int cur,int root){
	diss[cur]=0;
	for(int i=head[cur];i;i=e[i].next){
		int y=e[i].to;
		if(y==fa[cur]||vis[y])continue;
		dfs(y,cur);
		diss[cur]=max(diss[cur],diss[y]+e[i].dist);
	}
}
signed main(){
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>x>>y>>z;
		add(x,y,z),add(y,x,z);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)if(dis[i]>ans)ans=dis[i],dis[i]=0,s=i;
	ans=0;
	dfs(s,0);
	for(int i=1;i<=n;i++)if(dis[i]>ans)ans=dis[i],t=i;
	l=t,r=s;
	cout<<ans<<endl;
	int now=t;
	while(now!=s)vis[now]=1,son[fa[now]]=now,now=fa[now];
    now=t;
    while(now!=s){
        dfs2(now,0);
        if(diss[now]==ans-dis[now])l=now;
        now=fa[now];
    }
    now=s;
    while(now){
        dfs2(now,0);
        if(diss[now]==dis[now])r=now;
        now=son[now];
    }
    while(l!=r&&l){
        l=fa[l];
		ANS++;
    }
    cout<<ANS<<endl;
	return 0;
}
2023/6/17 15:07
加载中...