题意

给定一个n个点m条边的无向图,一开始点i的颜色为i,在第i+kn秒开始时,与节点i相邻的节点会被染成i的颜色(k为自然数)
定义D(i,j)第j秒结束时颜色为i的节点个数,求: $F(i)=\lim_{n -> \infty }{1\over n}\sum_{j=1}^{n}D(i,j)$

题解

首先模拟一轮染色过程,求出一轮下来,每个点最终会被染成开始时哪个点的颜色。这个染色结果是一个满射。将这个映射迭代无数次,就相当于染色无数轮。由于映射是满射,迭代的过程中,每个点的颜色会形成一个循环。求出这个循环,就知道每轮结束时每个点的颜色循环情况。最后再模拟一次染色过程,就能求出每个点在每个时刻的颜色循环情况。这样就能求出每个颜色的分数解,转成小数输出即可。

代码

看网上的代码,并不能AC
#include<bits/stdc++.h>
using namespace std; const int maxn = + ;
struct Edge{
int next, to;
}edges[*maxn];
int head[maxn], id; void add(int u, int v)
{
edges[id].to = v;
edges[id].next = head[u];
head[u] = id++;
} int n, m;
int ans[maxn];
int color[maxn];
bool vis[maxn];
int kind[maxn], color_num[maxn]; bool cmp(int x, int y)
{
return x > y;
} int main()
{
while(scanf("%d%d", &n, &m) == )
{
id = ;
//memset(head, -1, sizeof(head));
for(int i = ;i <= n;i++)
{
head[i] = -;
vis[i] = ;
color_num[i] = ;
ans[i] = ;
kind[i] = ;
color[i] = i;
}
//for(int i = 1;i <=n;i++) color[i] = i;
//memset(vis, 0, sizeof(vis));
//memset(color_num, 0, sizeof(color_num));
//memset(ans,0,sizeof(ans));
//memset(kind, 0, sizeof(kind));
for(int i = ;i < m;i++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
for(int i = ;i <= n;i++)
for(int j = head[i];j != -;j = edges[j].next)
{
int v = edges[j].to;
color[v] = color[i];
} for(int i = ;i <= n;i++)
vis[color[i]] = true; //哪些颜色出现过 int cnt = ;
for(int i = ;i <= n;i++)
if(vis[i]) kind[cnt++] = i; //第cnt种颜色为i for(int i = ;i <= n;i++)
color_num[color[i]]++; //for(int i = 1;i <= n;i++) printf("%d\n", kind[i]); for(int i = ;i <= n;i++)
{
for(int j = head[i];j != -;j = edges[j].next)
{
int v = edges[j].to;
color_num[color[v]]--; //把与i相连的颜色减去1
color[v] = color[i];
color_num[color[i]]++;
}
for(int j = ;j < cnt;j++) //对于每种颜色
ans[kind[j]] += color_num[kind[j]];
}
//sort(ans+1, ans+n+1, cmp);
for(int i = ;i <= n;i++)
{
if(ans[i]) printf("%.6f\n", ans[i]*1.0/n);
//else break;
}
}
return ;
}

参考链接:

1. https://blog.csdn.net/qq_37699336/article/details/83244519

2. https://www.zhihu.com/question/68280066

3. https://blog.csdn.net/alan_cty/article/details/78713865

 

2017icpc beijing-I题-Colored Nodes的更多相关文章

  1. 乘风破浪:LeetCode真题_025_Reverse Nodes in k-Group

    乘风破浪:LeetCode真题_025_Reverse Nodes in k-Group 一.前言 将一个链表按照一定的长度切成几部分,然后每部分进行翻转以后再拼接成一个链表是比较困难的,但是这也能锻 ...

  2. 乘风破浪:LeetCode真题_024_Swap Nodes in Pairs

    乘风破浪:LeetCode真题_024_Swap Nodes in Pairs 一.前言 这次还是链表的操作,不过我们需要交换链表奇数和偶数位置上的节点,因此要怎么做呢? 二.Swap Nodes i ...

  3. leetcode第24题--Reverse Nodes in k-Group

    problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...

  4. leetcode第23题--Swap Nodes in Pairs

    Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  5. 2017ICPC南宁 M题 The Maximum Unreachable Node Set【二分图】

    题意: 找出不能相互访问的点集的集合的元素数量. 思路: 偏序集最长反链裸题. 代码: #include<iostream> #include<cstring> using n ...

  6. 2017icpc乌鲁木齐网络赛Colored Graph (构造)

    题目 https://nanti.jisuanke.com/t/16958 题意 给定一个n(n<=500)个点的无向图,给每条边黑白染色,输出同色三角形最少的个数和对应的方案 分析 首先考虑给 ...

  7. 2017ICPC南宁补题

    https://www.cnblogs.com/2462478392Lee/p/11650548.html https://www.cnblogs.com/2462478392Lee/p/116501 ...

  8. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

  9. Leetcode中单链表题总结

    以下是个人对所做过的LeetCode题中有关链表类型题的总结,博主小白啊,若有错误的地方,请留言指出,谢谢. 一.有关反转链表 反转链表是在单链表题中占很大的比例,有时候,会以各种形式出现在题中,是比 ...

随机推荐

  1. [转帖]聊聊Web App、Hybrid App与Native App的设计差异

    聊聊Web App.Hybrid App与Native App的设计差异 https://www.cnblogs.com/zhuiluoyu/p/6056672.html 编者按:这3类主流应用你都了 ...

  2. golang gin框架设置静态目录

    router := gin.Default() 第一个参数是api 第二个静态问价的文件夹相对目录 router.StaticFS("/data", http.Dir(" ...

  3. Python基础笔记(四)

    1. 返回函数与闭包 如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure) def getSum(*args): def add(): ...

  4. Linux学习笔记之RAID笔记

    RAID: Redundant Arrays of Inexpensive Disks Independent Berkeley: A case for Redundent Arrays of Ine ...

  5. C#中精确计时的一点收获 Stopwatch

    http://www.cnblogs.com/jintianhu/archive/2010/09/01/1815031.html 参考: https://www.cnblogs.com/kissdod ...

  6. [個人紀錄] git 設定

    -- git history git config --global alias.history=log --graph --all --pretty=format:'%C(bold blue)%H% ...

  7. Centos7安装Tomcat7,并上传JavaWeb项目

    一.需要的工具(其他连接工具也行) 1.Xshell 2.XFTP 1.1首先将Tomcat7的压缩文件利用XFTP上传到Centos7系统上的 /etc/local/tomcat中 1.2 解压文件 ...

  8. ThreadLocal定义、使用案例及源码分析

    原文连接:(http://www.studyshare.cn/blog/details/1165/0 ) 一.ThreadLocal定义 jdk官方文档定义是:该类提供线程局部变量. 这些变量与其正常 ...

  9. Springboot vue.js html 跨域 前后分离 Activiti6 shiro 权限

    官网:www.fhadmin.org 特别注意: Springboot 工作流  前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...

  10. [摘抄] 2. module对象

    2. module对象 Node内部提供一个Module 构建函数,所有函数都是Module的实例. function Moudle(id,parent) { this.id = id; this.e ...