没打,简单补档

C.Edgy Trees

容斥,把黑边断掉数联通块,每个联通块贡献$siz^k$

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,mod=1e9+;
int n,k,t1,t2,t3,tot,aset[N],siz[N];
int Finda(int x)
{
return x==aset[x]?x:aset[x]=Finda(aset[x]);
}
int Qpow(int x,int k)
{
if(k<=) return k?x:;
int tmp=Qpow(x,k>>);
return 1ll*tmp*tmp%mod*((k&)?x:)%mod;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) aset[i]=i,siz[i]=;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
if(!t3)
{
int nx=Finda(t1),ny=Finda(t2);
aset[nx]=ny,siz[ny]+=siz[nx];
}
}
for(int i=;i<=n;i++)
if(aset[i]==i) (tot+=Qpow(siz[i],k))%=mod;
printf("%d",(Qpow(n,k)-tot+mod)%mod);
return ;
}

D.Steps to One

我写的辣鸡的$O(n\log n\sqrt n)$(并跑不满),太菜了

设dp[i]表示当前为i的期望步数,暴力DP即枚举1->m从gcd转移,改为枚举gcd(指所有因子),然后莫比乌斯函数统计1->n中和某个数互质的数的个数

 #include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define vint vector<int>
#define vit vector<int>::iterator
using namespace std;
const int N=,mod=1e9+;
int dp[N],pri[N],npr[N],mul[N];
int n,t,in,cnt,ans; vint fac[N];
void Add(int &x,int y)
{
x+=y;
if(x>=mod) x-=mod;
}
int Qpow(int x,int k)
{
if(k<=) return k?x:;
int tmp=Qpow(x,k>>);
return 1ll*tmp*tmp%mod*((k&)?x:)%mod;
}
void Pre()
{
in=Qpow(n,mod-);
for(int i=;i<=n;i++)
for(int j=*i;j<=n;j+=i)
fac[j].push_back(i);
npr[]=true,mul[]=;
for(int i=;i<=n;i++)
{
if(!npr[i]) pri[++cnt]=i,mul[i]=-;
for(int j=;j<=cnt&&(t=i*pri[j])<=n;j++)
{
npr[t]=true;
if(i%pri[j]==) break;
else mul[t]=-mul[i];
}
}
}
int Count(int x,int y)//Count:for i=1 to n,gcd(i,x)==y
{
int N=n/y,X=x/y,ret=;
for(int i=;i*i<=X;i++)
if(X%i==)
{
ret+=N/i*mul[i];
if(i*i!=X) ret+=N/(X/i)*mul[X/i];
}
return ret;
}
int main()
{
scanf("%d",&n),Pre(),dp[]=;
for(int i=;i<=n;i++)
{
int tmp=n/i;
for(vit it=fac[i].begin();it!=fac[i].end();it++)
t=*it,Add(dp[i],1ll*dp[t]*Count(i,t)%mod*in%mod);
dp[i]=1ll*(dp[i]+)*Qpow(n-tmp,mod-)%mod*n%mod;
}
for(int i=;i<=n;i++) Add(ans,dp[i]);
printf("%lld",1ll*ans*in%mod);
return ;
}

呃,发现题解也不怎么快,他是用2^质因子个数容斥算的,$2^6*6$怕不是跟非常跑不满的根号差不多

E.Maximize Mex

显然答案单调不升,把每种潜力值和每个club看做左右部点,倒着加边,每次在上次的基础上继续二分图匹配到没有增广路为止

注意潜力值是从零开始的,还有每次跑完记得更新dfn

 #include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define vint vector<int>
#define vit vector<int>::iterator
using namespace std;
const int N=;
int n,m,q,t1,t2,dfn;
int val[N],bel[N],lft[N];
int del[N],vis[N],mth[N],ans[N]; vint ve[N];
bool DFS(int nde)
{
int t;
for(vit it=ve[nde].begin();it!=ve[nde].end();it++)
if(vis[t=*it]!=dfn)
{
vis[t]=dfn;
if(mth[t]==-||DFS(mth[t]))
{mth[t]=nde; return true;}
}
return false;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&val[i]);
for(int i=;i<=n;i++) scanf("%d",&bel[i]);
for(int i=;i<=m;i++) mth[i]=-; dfn=;
scanf("%d",&q);
for(int i=;i<=q;i++)
scanf("%d",&lft[i]),del[lft[i]]=true;
for(int i=;i<=n;i++)
if(!del[i]) ve[val[i]].push_back(bel[i]);
for(int i=q,mex=-;i;i--)
{
while(DFS(mex+)) mex++,dfn++; ans[i]=mex+,dfn++;
ve[val[lft[i]]].push_back(bel[lft[i]]);
}
for(int i=;i<=q;i++) printf("%d\n",ans[i]);
return ;
}

F.Dish Shopping

Codeforces Round #548的更多相关文章

  1. Codeforces Round 548 (Div. 2)

    layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

    https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\), ...

  3. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

  4. CodeForces Round #548 Div2

    http://codeforces.com/contest/1139 A. Even Substrings You are given a string s=s1s2…sns=s1s2…sn of l ...

  5. Codeforces Round #548 (Div. 2) C dp or 排列组合

    https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...

  6. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #548 (Div. 2) D 期望dp + 莫比乌斯反演

    https://codeforces.com/contest/1139/problem/D 题意 每次从1,m中选一个数加入队列,假如队列的gcd==1停止,问队列长度的期望 题解 概率正着推,期望反 ...

  8. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  9. Codeforces Round #548 (Div. 2) C. Edgy Trees

    You are given a tree (a connected undirected graph without cycles) of 

随机推荐

  1. Wechat login authorization(OAuth2.0)

    一.前言 昨天小组开了个会,让我今天实现一个微信网页授权的功能,可以让用户在授权之后无需再次登录既可进入用户授权界面.在这之前我也从没接触过微信公众号开发之类的,也不知道公众号后台是啥样子的,自己所在 ...

  2. Nginx支持WebSocket反向代理-学习小结

    WebSocket是目前比较成熟的技术了,WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选择.其为HTML5的一部分,WebSocket相较于原来开发这类app的 ...

  3. 软件工程导论课后习题Github作业(把一个英文句子中的单词次序逆序,单词中字母正常排列)

    Java源代码    package yly; import java.util.Scanner; public class ruanjian { public static void main(St ...

  4. Leetcode——53.最大子序和

    @author: ZZQ @software: PyCharm @file: leetcode53_最大子序和.py @time: 2018/11/26 12:39 要求:给定一个整数数组 nums ...

  5. Laravel Exception处理逻辑解析

    Laravel Exception处理逻辑解析 vendor/laravel/framework/src/Illuminate/Foundation/Application.php app首先继承了c ...

  6. yii框架通过IP地址来使用gii

    这里使用的YII框架的版本是2.0.13 详情请参考官方文档:用Gii生成代码 使用gii的主要步骤 1.生成模型(Model Generator) 2.生成CRUD代码 注意点 1.在生成CURD代 ...

  7. HDU 2052 Picture

    http://acm.hdu.edu.cn/showproblem.php?pid=2052 Problem Description Give you the width and height of ...

  8. python中Switch/Case实现

    学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...

  9. grep 匹配打印的上下几行

    如果在只是想匹配模式的上下几行,grep可以实现.   $grep -5 'parttern' inputfile //打印匹配行的前后5行   $grep -C 5 'parttern' input ...

  10. BZOJ3829[Poi2014]FarmCraft——树形DP+贪心

    题目描述 In a village called Byteville, there are   houses connected with N-1 roads. For each pair of ho ...