QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#366985#3100. Meetings 2Rikku_eq0 2ms20080kbC++142.4kb2024-03-25 15:33:452024-03-25 15:33:45

Judging History

你现在查看的是最新测评结果

  • [2024-03-25 15:33:45]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:20080kb
  • [2024-03-25 15:33:45]
  • 提交

answer

#include <bits/stdc++.h>
#define N 200005
#define mxL 20
using namespace std;
typedef long long ll;

int n, L, lg2[N*2], rec[N], stk[N];
int sz[N], d[N], tI[N], id[N*2], tmr;
int st[N][mxL], stD[N][mxL], p[N][mxL];
vector <int> vec[N], to[N];

void dfs (int u, int fa)
{
    sz[u]=1; d[u]=d[fa]+1;
    tI[u]=++tmr; id[tmr]=d[u];

    p[u][0]=fa;
    for (int i=1; i<=L; i++) { p[u][i]=p[p[u][i-1]][i-1]; }

    for (int i=0; i<(int)to[u].size(); i++) {
        int v=to[u][i];
        if (v==fa) { continue; }
        dfs(v, u); sz[u]+=sz[v];
        id[++tmr]=d[u];
    }
    vec[sz[u]].push_back(u);
}

inline int mn (int l, int r) { return min(st[l][lg2[r-l+1]], st[r-(1<<lg2[r-l+1])+1][lg2[r-l+1]]); }
inline int mxD (int l, int r) { return max(stD[l][lg2[r-l+1]], stD[r-(1<<lg2[r-l+1])+1][lg2[r-l+1]]); }

int main ()
{
    // freopen("0test.in", "r", stdin);
    // freopen("0test.out", "w", stdout);

    scanf("%d", &n);
    L=ceil(log(n*2)/log(2));
    for (int i=1; i<n; i++) {
        int u, v; scanf("%d %d", &u, &v);
        to[u].push_back(v);
        to[v].push_back(u);
    }
    for (int i=2; i<=n*2; i++) { lg2[i]=lg2[i>>1]+1; }
    dfs(1, 0);

    for (int j=tmr; j>=1; j--) {
        st[j][0]=d[id[j]];
        for (int k=1; j+(1<<(k-1))<=tmr; k++) {
            st[j][k]=min(st[j][k-1], st[j+(1<<(k-1))][k-1]);
        }
    }

    for (int i=1; i<=n; i++) {
        if (i&1) { printf("1\n"); continue; }

        int t=i/2;
        while (!vec[t].size()) { t++; }

        int res=-n-1, ans=0;
        int tp=0, len=vec[t].size();

        for (int j=len-1; j>=0; j--) {
            stD[j][0]=d[vec[t][j]];
            for (int k=1; j+(1<<(k-1))<len; k++) {
                stD[j][k]=max(stD[j][k-1], stD[j+(1<<(k-1))][k-1]);
            }
            int u=vec[t][j];
            int dep=d[u];
            for (int k=L; k>=0; k--) {
                if (p[u][k] && sz[p[u][k]]<=n-(t)) { u=p[u][k]; }
            }
            ans=max(ans, dep-d[u]+1);
        }
        for (int j=1; j<len; j++) {
            int u=vec[t][j];
            rec[j]=mn(tI[vec[t][j-1]], tI[u]);
            while (tp>0 && rec[stk[tp]]>rec[j]) { tp--; }
            // (stk[tp], j-1)
            res=max(res, mxD(stk[tp], j-1)-rec[j]*2);
            ans=max(ans, res+d[u]);
            stk[++tp]=j;
        }

        ans++; printf("%d\n", ans);
    }

    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 4
Accepted
time: 0ms
memory: 17868kb

input:

1

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 2ms
memory: 20080kb

input:

2
2 1

output:

1
2

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 2ms
memory: 18420kb

input:

3
1 3
3 2

output:

1
3
1

result:

ok 3 lines

Test #4:

score: -4
Wrong Answer
time: 2ms
memory: 19520kb

input:

14
12 14
12 9
14 2
12 6
12 7
6 4
3 4
1 4
12 8
13 1
10 12
11 6
6 5

output:

1
8
1
5
1
2
1
2
1
2
1
2
1
2

result:

wrong answer 2nd lines differ - expected: '7', found: '8'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%