TLE on #9#13#31#32求助
查看原帖
TLE on #9#13#31#32求助
260061
Karl_Aurora楼主2023/10/31 09:55

rt,注释提交得到问题出在求每步操作的地方(两个getnxt函数及主程序对应循环),考虑到其它点正常,应该是出现了死循环,但是看不出有什么地方会导致死循环,求大佬帮忙看一下,代码:

#include <bits/stdc++.h>
#define maxn 500010
using namespace std;
template < typename T >
inline void read(T &X)
{
    X = 0; bool f = false; char ch = getchar();
    while (!isdigit(ch)) {f |= ch == '-'; ch = getchar();}
    while (isdigit(ch)) {X = (X * 10) + (ch ^ 48); ch = getchar();}
    X = f ? -X : X;
}
int s[maxn];
int fa[maxn];
int getfa(const int &x) {return fa[x] = (fa[x] == x ? x : getfa(fa[x]));}
struct side {int to, next;} sidelist[maxn << 1];
int sidecnt, sidehead[maxn];
inline void buildside(const int &u, const int &v) {sidelist[++sidecnt] = {v, sidehead[u]}; sidehead[u] = sidecnt;}
int n, m;
int siz[maxn], maxsiz[maxn];
int rt;
void dfs(const int &x)
{
    siz[x] = 1;
    for (int i = sidehead[x]; i; i = sidelist[i].next)
    {
        int to = sidelist[i].to;
        if (!siz[to]) 
        {
            dfs(to);
            siz[x] += siz[to]; maxsiz[x] = max(maxsiz[x], siz[to]);
        }
    }
    if (max(maxsiz[x], n - siz[x]) <= (n >> 1)) rt = x;
}
bool vis[maxn];
stack < int > s1, s2;
vector < int > opx, opt;
bool f;
inline void getnxt1()
{
    while (!s1.empty())
    {
        int x = s1.top();
        for (int i = sidehead[x]; i; i = sidelist[i].next)
        {
            int to = sidelist[i].to;
            if (vis[to]) continue;
            vis[to] = true;
            s1.emplace(to); opx.emplace_back(to); opt.emplace_back(f ? 2 : 1);
            return;
        }
        s1.pop();
        if(!s1.empty()) {opx.emplace_back(s1.top()); opt.emplace_back(f ? 2 : 1);}
    }
    return;
}
inline void getnxt2()
{
    while (!s2.empty())
    {
        int x = s2.top();
        for (int i = sidehead[x]; i; i = sidelist[i].next)
        {
            int to = sidelist[i].to;
            if (vis[to]) continue;
            vis[to] = true;
            s2.emplace(to); opx.emplace_back(to); opt.emplace_back(f ? 1 : 2);
            return;
        }
        s2.pop();
        if(!s2.empty()) {opx.emplace_back(s2.top()); opt.emplace_back(f ? 1 : 2);}
    }
    return;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("P8119.in", "r", stdin);
    freopen("P8119.out", "w", stdout);
#endif
    read(n); read(m);
    for (int i = 1; i <= n; ++i) fa[i] = i, s[i] = 1;
    for (int i = 1; i <= m; ++i)
    {
        int u, v;
        read(u); read(v);
        int fu = getfa(u), fv = getfa(v);
        if (fu != fv)
        {
            buildside(u, v); buildside(v, u);
            if (s[fu] > s[fv]) swap(fu, fv);
            fa[fu] = fv; s[fv] += s[fu];
        }
    }
    dfs(1);
    vis[rt] = 1;
    s1.emplace(rt); s2.emplace(rt);
    while (!s1.empty() || !s2.empty())
    {
        getnxt1(); getnxt2();
        if (!s1.empty() || !s2.empty()) {opx.emplace_back(0); opt.emplace_back(3);}
        f = !f;
    }
    printf("%d %llu\n", rt, opx.size());
    for (int i = 0; i < opx.size(); ++i)
    {
        if (opt[i] == 3) printf("Swap\n");
        else {printf(opt[i] == 1 ? "Ran" : "Chen"); printf(" %d\n", opx[i]);}
    }
    return 0;
}
2023/10/31 09:55
加载中...