RE求助
查看原帖
RE求助
260061
Karl_Aurora楼主2023/10/28 16:19

Rt,按理来说数组不会越界,访问栈顶前也检查了是否为空,但还是出现了段错误和MLE,求帮忙看一眼代码

#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 fa[maxn];
int getfa(const int &x) {return fa[x] = (fa[x] == x ? x : getfa(fa[x]));}
struct side {int to, next;} sidelist[maxn];
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;
    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);
            fa[fu] = fv;
        }
    }
    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/28 16:19
加载中...