ACM-ICPC Beijing Division 2017 Network Synchronized Competition I

http://hihocoder.com/contest/icpcbeijing2017/problem/9

Find the average number of occurrences of each color

First simulate a round of 1-n, the last color of point x is recorded as fax, which means that the next 1-n, the color of point x depends on the color of the current fax

Then it will be found that this is a ring tree, we first get the size of the inner ring of a connected block, we must know that only the color on the ring is useful, and the contribution is the same, because only the color on the ring will keep spinning, and the colors in other places will be is covered, and the loop node of this connecting block is n* the size of the loop. Consider a cycle of 1-n. If the color of point a is 1, then the distance from point a is an integer multiple of the size of the ring. 1. In this way, a division is made on the connecting block. When we consider that the loop node is the size of the n* ring, the color 1 will appear once at the point of each connecting block, without repetition or omission, so we put (n* ring size) is flattened into n operations,

Considering an edge of a point in rounds 1-n, if u and v belong to the same connected block, then it has no effect on the answer statistics. Suppose u and v are the colors 1 and 2 of the same connected block. If The current operation dyes color 2 to 1, then there will always be a time when color 1 will be dyed the color of the same connection block on v, and the sum does not change, so we focus on the operation between different connection blocks, when a connection block Because when the current operation is one point less, we need to process the correct time between the last and now of this Unicom block, that is, the elapsed time * the point tree at each time = the total number of points in this period, and maintain it with last and nowsize total points

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
typedef long  long int ll;
const int maxn = 1e5+9;
int fa [maxn], vis [maxn];
int color[maxn],ccnt;
int last[maxn],cnt[maxn];
int size[maxn];
ll cot[maxn];
int nowsize[maxn];
double years[maxn];
stack<int> stak;
vector<int> vv[maxn];
void cal(int now,int color){
    cot[color]+=(ll)(now-last[color])*nowsize[color];
    last[color] = now;
}
int main(int argc, const char * argv[]) {
    int n,m;
    while (~scanf("%d%d",&n,&m)) {
        for(int i=1;i<=n;i++){
            vv[i].clear();
            fa [i] = i;
            show [i] = 0;
            color[i] = 0;
            last[i] = 0;
            cnt[i] = 0;
            cot[i] = 0;
            nowsize[i] = 0;
        }
        ccnt = 0;
        for(int i=1;i<=m;i++){
            int u, v;
            scanf("%d%d",&u,&v);
            vv[u].push_back(v);
            vv[v].push_back(u);
        }
        for(int i=1;i<=n;i++){
            for(int v: vv[i]){
                fa [v] = fa [i];
            }
        }
        for(int i=1;i<=n;i++){
            if(vis[i]==0){
                int x = i;
                while (!stak.empty()) {
                    stak.pop ();
                }
                while (vis[x]==0) {
                    stak.push(x);
                    screw[x] = 1;
                    if (vis [fa [x]] == 0)
                    x = fa [x];
                }
                x = fa [x];
                if(color[x]!=0) {
                    while (!stak.empty()) {
                        int t = stak.top();
                        color[t] = color[x];
                        stak.pop ();
                    }
                }else{
                    ccnt++;
                    size[ccnt] = 1;
                    while (stak.top()!=x) {
                        size[ccnt]++;
                        int t = stak.top();
                        color[t] = ccnt;
                        stak.pop ();
                    }
                    while (!stak.empty()) {
                        int t = stak.top();
                        color[t] = ccnt;
                        stak.pop ();
                    }
                }
            }
        }
        for(int i=1;i<=n;i++){
            nowsize[color[i]]++;
            
        }
        for(int i=1;i<=n;i++){
            for(int v: vv[i]){
                if(color[i]!=color[v]){
                    cal(i-1, color[i]);
                    cal(i-1, color[v]);
                    nowsize[color[i]]++;
                    nowsize[color[v]]--;
                    color[v] = color[i];
                }
            }
        }
        for(int i=1;i<=ccnt;i++){
            cal(n, i);
        }
        int pp = 0;
        for(int i=1;i<=ccnt;i++){
            for(int j=0;j<size[i];j++){
                ans[pp++] =cot[i] *1.0/n/size[i];
            }
        }
        sort(years,years+pp);
        for(int i=pp-1;i>=0;i--){
            printf("%.6f\n",ans[i]);
        }
    }
    return 0;
}

/*
 6 6
 1 3
 3 5
 5 2
 2 4
 4 6
 6 1
 
 */

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324592454&siteId=291194637