QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#645573#9474. Colored NodesAfterlife#AC ✓225ms14964kbC++203.1kb2024-10-16 19:01:532024-10-16 19:01:55

Judging History

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

  • [2024-10-16 19:01:55]
  • 评测
  • 测评结果:AC
  • 用时:225ms
  • 内存:14964kb
  • [2024-10-16 19:01:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int n , m;
vector<int> E[100005];
int fa[100005];
int col[100005];

typedef long long ll;
int main() {
    ios::sync_with_stdio(false) ; cin.tie(0) ;
    while(cin >> n >> m) {
        for(int i = 1;i <= n;i++) E[i].clear() , fa[i] = 0 , col[i] = 0;
        for(int i = 1;i <= m;i++) {
            int u , v;cin >> u >> v;
            E[u].push_back(v) ;
            E[v].push_back(u);
        }
        vector<ll> contri(n + 1);
        vector<int> mn(n + 1);
        for(int i = 1;i <= n;i++) {
            sort(E[i].begin() , E[i].end()) ;
            if(!E[i].size()) continue ;
            E[i].erase(unique(E[i].begin() , E[i].end()) , E[i].end()) ;
            for(int j = 0;j < E[i].size();j++) {
                if(E[i][j] == i) {
                    E[i].erase(E[i].begin() + j) ; break ;
                }
            }
            if(E[i][0] < i) {
                auto it = lower_bound(E[i].begin() , E[i].end() , i);
                it-- ;
                fa[i] = (*it);
            }
            else {
                auto it = E[i].end() ;
                it-- ;
                fa[i] = (*it) ;
                mn[i] = 1;
            }
        }
        
        vector<double> ans ;
        for(int i = 1;i <= n;i++) {
            // printf("%d %d\n",i,fa[i]);
            if(!E[i].size()) {
                ans.push_back(1.0); continue ;
            }
            for(int j = 0 ; j < E[i].size();j++) {
                if(j < E[i].size() - 1) contri[E[i][j]] += (E[i][j + 1] - E[i][j]) ;
                else contri[E[i][j]] += (E[i][0] + n - E[i][j]) ;
            }
        }

        vector<int> vis(n + 1) , to(n + 1);
        int cc = 0;
        map<int , vector<int> > mp;
        vector<ll> val(n + 1);

        for(int i = 1;i <= n;i++) {
            if(vis[i]) continue ;
            int u = i;
            vector<int> cl;
            while(!vis[u]) {
                vis[u] = i;
                cl.push_back(u);
                u = fa[u] ;
            }
            if(vis[u] != i) {
                for(auto x : cl) to[x] = to[u] ;
            }
            else {
                ++cc ;
                for(auto x : cl) to[x] = cc;
                int v = fa[u];
                if(mn[u]) mp[cc].push_back(u);
                // printf("%d %d\n",u,mn[u]);
                while(v != u) {
                    if(mn[v]) mp[cc].push_back(v) ;
                    // printf("%d %d\n",v,mn[v]);
                    v = fa[v];
                }
            }
        }
        for(int i = 1;i <= n;i++) {
            val[to[i]] += contri[i];
            // printf("%d %d : %d\n",i,contri[i] , to[i]) ;
        }
        // printf("MPS %d : %d\n",mp.size() , cc);
        for(auto &[x,y] : mp) {
            // printf("%d : %d\n",x,y.size()) ;
            for(auto z : y) ans.push_back((double)val[x] / n / y.size()) ;
        }

        sort(ans.begin() , ans.end()) ;
        for(int i = ans.size() - 1;i >= 0;i--) {
            cout << fixed << setprecision(6) << ans[i] << '\n';
        }
        // break ;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3960kb

input:

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

output:

3.000000
2.000000
2.800000
2.200000

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 225ms
memory: 14964kb

input:

10 10
1 2
2 3
3 4
4 5
5 6
10 7
7 8
8 9
9 10
6 9
10 10
6 5
5 3
3 4
4 2
6 1
10 7
7 8
8 9
9 10
6 10
4 3
1 2
2 3
3 4
8 12
4 7
7 6
7 2
7 3
2 5
5 7
4 3
7 2
7 6
3 4
4 5
4 8
100 100
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25...

output:

5.600000
4.400000
6.000000
4.000000
4.000000
7.000000
1.000000
51.540000
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
3.028750
62.400000
15.660000
5.290000
3.930000
3.430000
2.500000
2.040000
2.000000
1.430000
...

result:

ok 120464 lines