QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#344969#8055. BalanceinstallbTL 4ms57032kbC++144.6kb2024-03-05 21:17:412024-03-05 21:17:41

Judging History

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

  • [2024-03-05 21:17:41]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:57032kb
  • [2024-03-05 21:17:41]
  • 提交

answer

// ECFinal 2023 I
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000005;
const int INF = 0x3f3f3f3f;

int ec,to[N << 1],nxt[N << 1],hed[N];
void add_edge(int f,int t){
    ++ ec; to[ec] = t; nxt[ec] = hed[f]; hed[f] = ec;
}

int dfc,dfn[N],low[N],bri[N << 1];
void tarjan(int u,int fe){
    dfn[u] = low[u] = ++ dfc;
    for(int v,i = hed[u];i;i = nxt[i]){
        v = to[i];
        if(!dfn[v]){
            tarjan(v,i);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u]) bri[i] = bri[i ^ 1] = 1;
        }
        else if(i != (fe ^ 1)) low[u] = min(low[u],dfn[v]);
    }
}

int col[N],cco,siz[N];
void dfs_dcc(int u,int id){
    col[u] = id;
    siz[col[u]] ++;
    for(int v,i = hed[u];i;i = nxt[i]){
        v = to[i];
        if(bri[i] || col[v]) continue;
        dfs_dcc(v,id);
    }
}

// get DCC

int n,m,a[N],eu[N],ev[N];
vector <int> G[N];
int wu[N],fa[N];

void dfs_sz(int u,int f){
    fa[u] = f;
    for(int v : G[u]){
        if(v == f) continue;
        dfs_sz(v,u);
        siz[u] += siz[v];
        if(a[n - siz[v]] != a[n - siz[v] + 1]) wu[v] |= 1; // u -> v
        if(a[siz[v]] != a[siz[v] + 1]) wu[v] |= 2; // rev v -> u
    }
}

pair <int,int> f[N],g[N];
int ans = 0;
tuple <int,int,int> ansp;
void dfs_dp(int u){
    f[u] = g[u] = {0,u};
    for(int v : G[u]){
        if(v == fa[u]) continue;
        dfs_dp(v);
        int fv = f[v].first + (wu[v] & 1);
        int gv = g[v].first + (wu[v] >> 1);
        if(fv + g[u].first > ans){
            ans = fv + g[u].first;
            ansp = {g[u].second,u,f[v].second};
        }
        if(gv + f[u].first > ans){
            ans = gv + f[u].first;
            ansp = {g[v].second,u,f[u].second};
        }
        f[u] = max(f[u],make_pair(fv,f[v].second));
        g[u] = max(g[u],make_pair(gv,g[v].second));
    }
}

int anscol[N];
void init(){
    ec = 1; dfc = 0; cco = 0;
    ans = 0;
    for(int i = 1;i <= n;i ++){
        hed[i] = dfn[i] = anscol[i] = col[i] = siz[i] = wu[i] = fa[i] = 0;
        f[i] = g[i] = {0,0};
        G[i].clear();
    }
    for(int i = 1;i <= (m * 2 + 1);i ++) bri[i] = 0;
}

void solve(){
    cin >> n >> m;
    init();
    for(int u,v,i = 1;i <= m;i ++){
        cin >> u >> v;
        eu[i] = u; ev[i] = v;
        add_edge(u,v); add_edge(v,u);
    }
    for(int i = 1;i <= n;i ++) cin >> a[i];
    sort(a + 1,a + 1 + n);
    for(int i = 1;i <= n;i ++) if(!dfn[i]) tarjan(i,0);
    for(int i = 1;i <= n;i ++){
        if(!col[i]){
            cco ++; dfs_dcc(i,cco);
        }
    }
    vector <pair <int,int> > E;
    for(int i = 1;i <= m;i ++){
        if(col[eu[i]] != col[ev[i]]) E.push_back({min(col[eu[i]],col[ev[i]]),max(col[eu[i]],col[ev[i]])});
    }
    sort(E.begin(),E.end());
    E.erase(unique(E.begin(),E.end()),E.end());
    for(auto [u,v] : E){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs_sz(1,0);
    dfs_dp(1);

    auto [su,lu,tu] = ansp;
    // for(int i = 1;i <= n;i ++) cout << col[i] << " \n"[i == n];
    // for(int i = 1;i <= n;i ++) cout << siz[i] << " \n"[i == n];
    // for(int i = 1;i <= n;i ++) cout << wu[i] << " \n"[i == n];
    // for(int i = 1;i <= n;i ++) cout << fa[i] << " \n"[i == n];
    // cout << ans << ' ' << su << ' ' << lu << ' ' << tu << endl;
    vector <int> lisa;
    for(int i = 1;i <= n;i ++){
        if(i == 1 || a[i] != a[i - 1]) lisa.push_back(a[i]);
    }
    if(ans + 1 < lisa.size()){
        cout << "No\n";
        return;
    }
    assert(ans + 1 == lisa.size());

    // calc method
    int cur = 1;
    queue <int> q;
    while(su != lu){
        if(wu[su] >> 1){
            anscol[su] = cur;
            anscol[fa[su]] = cur + 1;
            cur ++;
            q.push(su); q.push(fa[su]);
        }
        su = fa[su];
    }
    vector <pair <int,int> > vp;
    while(tu != lu){
        if(wu[tu] & 1){
            vp.push_back({tu,fa[tu]});
        }
        tu = fa[tu];
    }
    reverse(vp.begin(),vp.end());
    for(auto [u,f] : vp){
        anscol[f] = cur;
        anscol[u] = cur + 1;
        cur ++;
        q.push(f); q.push(u);
    }
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int v : G[u]){
            if(!anscol[v]){
                anscol[v] = anscol[u];
                q.push(v);
            }
        }
    }
    cout << "Yes\n";
    for(int i = 1;i <= n;i ++) cout << anscol[col[i]] << " \n"[i == n];
}

int main(){
    ios::sync_with_stdio(false);
    int TC; cin >> TC;
    while(TC --) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 57032kb

input:

5
5 4
1 2
2 3
3 4
4 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 2 2 3
5 6
1 2
1 2
2 3
3 4
4 5
3 5
1 2 1 2 1
2 2
1 2
1 2
1 2

output:

Yes
1 2 3 4 5
No
Yes
2 1 3 2 2
Yes
2 2 1 1 1
No

result:

ok ok (5 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

100000
4 3
4 2
1 3
2 1
2 1 3 2
5 7
2 5
5 3
2 3
5 1
4 3
2 4
4 3
1 3 1 1 2
3 2
2 1
2 3
1 1 1
4 7
3 4
1 4
2 3
4 3
4 2
3 1
4 3
2 3 3 2
4 6
2 4
3 1
1 2
1 2
2 3
4 2
1 1 3 3
4 7
3 2
4 2
1 2
3 4
3 2
2 4
3 4
2 1 1 1
5 5
3 2
1 5
4 5
4 3
2 1
1 2 2 3 2
3 5
2 3
1 3
3 1
3 1
2 1
3 2 3
2 3
2 1
2 1
1 2
1 1
2 3
2 1
1...

output:

Yes
2 2 3 1
No
Yes
0 0 0
No
No
Yes
2 1 1 1
No
No

result: