求大家帮我优化,TLE了后面几个,孩子已经不知道怎么优化了
查看原帖
求大家帮我优化,TLE了后面几个,孩子已经不知道怎么优化了
817044
cjwdyzxfblzs楼主2023/6/22 21:44
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const int N = 1e6 + 1e5;
int n, m;
bitset<N> st;
vector<int> dir[N], graph[N];
int dfn[N], low[N], timestamp, stk[N], top, scc_cnt, id[N], sizes[N], sub[N], cnt, direct; bool in_stk[N];
inline void tarjan(int u, int fa)
{
    dfn[u] = low[u] = ++timestamp;
    stk[++top] = u, in_stk[u] = true;
    for (auto j : graph[u])
    {
        if (j == fa)
            continue;
        if (!dfn[j])
        {
            tarjan(j, u);
            low[u] = min(low[u], low[j]);
        }
        else if (in_stk[j])
            low[u] = min(low[u], low[j]);
    }
    if (dfn[u] == low[u])
    {
        int y;
        scc_cnt++;
        do
        {
            y = stk[top--];
            in_stk[y] = false;
            id[y] = scc_cnt;
            dir[scc_cnt].push_back(y);
            sizes[scc_cnt]++;
        } while (y != u);
        if (sizes[scc_cnt] >= 2)
            direct = scc_cnt;
    }
}
inline void dfs(int now, vector<int> *graph)
{
    for (auto v : graph[now])
    {
        if (st[v])
            continue;
        st[v] = true;
        sub[++cnt] = v;
        dfs(v, graph);
    }
}
inline void dfs(int u, int fa)
{
    sub[++cnt] = u;
    for (auto v : graph[u])
    {
        if (v == fa)
            continue;
        if (v == false || v == 0x3f3f3f3f)
            continue;
        dfs(v, u);
    }
}
int ans[N];
signed main()
{
    n = read(), m = read();
    for (int i = 1; i <= m; i++)
        dir[i].push_back(0x3f3f3f3f);
    for (int i = 1, u, v; i <= m; i++)
    {
        u = read(), v = read();
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    for (int i = 1; i <= n; i++)
        sort(graph[i].begin(), graph[i].end());
    if (m == n - 1)
    {
        st[1] = true;
        sub[++cnt] = 1;
        dfs(1, graph);
        for (int i = 1; i <= cnt; i++)
            cout << sub[i] << " ";
        exit(0);
    }
    if (m == n)
    {
        memset(ans, 127, sizeof(ans));
        for (int i = 1; i <= n; i++)
            if (!dfn[i])
                tarjan(i, i);
        for (int i = 1; i <= sizes[direct]; i++)
        {
            cnt = 0;
            int now = dir[direct][i], ne = dir[direct][i % sizes[direct] + 1], w1, w2;
            for (int j = 0; j < graph[now].size(); j++)
                if (graph[now][j] == ne)
                {
                    w1 = j;
                    graph[now][j] = 0;
                    break;
                }
            for (int j = 0; j < graph[ne].size(); j++)
                if (graph[ne][j] == now)
                {
                    w2 = j;
                    graph[ne][j] = 0;
                    break;
                }
            dfs(1, 0);
            graph[now][w1] = ne;
            graph[ne][w2] = now;
            for (int j = 1; j <= n; j++)
            {
                if (ans[j] < sub[j]) break;
                if (ans[j] > sub[j])
                {
                    for (int k = j; k <= n; k++)
                        ans[k] = sub[k];
                    break;
                }
                ans[j] = sub[j];
            }
        }
        for (int i = 1; i <= n; i++)
            cout << ans[i] << " ";
        exit(0);
    }
    return 0;
}
2023/6/22 21:44
加载中...