codeforces 702E Analysis of Pathes in Functional Graph

直接倍增就好了。。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 100500
#define inf 0x7f7f7f7f7f7f7f7fLL
using namespace std;
long long n,k,anc[maxv][35],mn[maxv][35],w[maxv][35],table[35];
int main()
{
    table[0]=1;for (long long i=1;i<=34;i++) table[i]=table[i-1]*2;
    scanf("%I64d%I64d",&n,&k);
    for (long long i=0;i<n;i++) scanf("%I64d",&anc[i][0]);
    for (long long i=0;i<n;i++) {scanf("%I64d",&mn[i][0]);w[i][0]=mn[i][0];}
    for (long long e=1;e<=34;e++)
        for (long long i=0;i<n;i++)
        {
            anc[i][e]=anc[anc[i][e-1]][e-1];
            mn[i][e]=min(mn[i][e-1],mn[anc[i][e-1]][e-1]);
            w[i][e]=w[i][e-1]+w[anc[i][e-1]][e-1];
        }
    for (long long i=0;i<n;i++)
    {
        long long ret1=0,ret2=inf,ret3=k,now=i;
        for (long long e=34;e>=0;e--)
        {
            if (ret3>=table[e])
            {
                ret3-=table[e];
                ret1+=w[now][e];ret2=min(ret2,mn[now][e]);
                now=anc[now][e];
            }
        }
        printf("%I64d %I64d\n",ret1,ret2);
    }
    return 0;
}
时间: 2024-10-12 21:15:45

codeforces 702E Analysis of Pathes in Functional Graph的相关文章

codeforces 702E Analysis of Pathes in Functional Graph(倍增)

E. Analysis of Pathes in Functional Graph You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1. Graph is given as the array f0, f1, ..., fn - 1, where fi 

CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上差分 ] | Codeforces Round #429(Div 1) 题意: 选择一个边集合,满足某些点度数的奇偶性 分析: 将d = 1的点连成一颗树,不在树上的点都不连边. 可以发现,当某个节点u的所有子节点si均可操控 (u, si) 来满足自身要求 即整棵树上至多只有1个点不满足自身要求,

Codeforces Round #261 (Div. 2)——Pashmak and Graph

题目链接 题意: n个点,m个边的有向图,每条边有一个权值,求一条最长的路径,使得路径上边值严格递增.输出路径长度 (2?≤?n?≤?3·105; 1?≤?m?≤?min(n·(n?-?1),?3·105)) 分析: 因为路径上会有重复点,而边不会重复,所以最开始想的是以边为状态进行DP,get TLE--后来想想,在以边为点的新图中,边的个数可能会很多,所以不行. 考虑一下裸的DP如何做:路径上有重复点,可以将状态详细化,dp[i][j]表示i点以j为结束边值的最长路,但是数据不允许这样.想想

【题解】CF739D Recover a functional graph(构造+二分图匹配)

[题解]CF739D Recover a functional graph(构造+二分图匹配) 题目大意 一个图被称为F图当且仅当每个点出度为1.可以发现这个图是一个内向基环森林,给出所有点到它能到达的环(只会有一个)的距离dis和那个环的长度len,然而有些点的信息模糊了,用?代替,可以是任何数.现在你要输出一个合法的构造方案. 可以提炼出一个图是F图的条件: 对应环的距离数组是自然数数列 \(len=k\)的点的个数是\(tk,t\in N^+\) 由于第二个条件只能用问号补,没有决策需要做

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces 566 F. Clique in the Divisibility Graph

Codeforces 566F 的传送门 As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively. Just in case, let us remind you that a clique in a non-directed graph is

Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组(u,v,s)表示u到v的(不一定为简单路径)路径上xor值为s.求出这张无向图所有不重复三元组的s之和.1≤n≤10^5,1≤m≤2*10^5. 想法: 如果做过[Wc2011 xor]这道题目(题解),那么问题变得简单起来了. ①假设我们钦定一个(u,v),设任意一条u->v的路径xor值为X,

Codeforces 1186F - Vus the Cossack and a Graph 模拟乱搞/欧拉回路

题意:给你一张无向图,要求对这张图进行删边操作,要求删边之后的图的总边数 >= ceil((n + m) / 2), 每个点的度数 >= ceil(deg[i] / 2).(deg[i]是原图中i的度数) 思路1:模拟 + 乱搞 直接暴力删就行了,读入边之后随机打乱一下就很难被hack了. 代码: #include <bits/stdc++.h> #define LL long long #define INF 0x3f3f3f3f #define db double #defin

CF739D Recover a functional graph

Link 首先把所有的按环长分类,那么我们需要做的就是满足以下几个条件: \(1.\)每个环长的到环距离为\(0\)的点的个数应该是环长的倍数.当然只需要在最低限度下将其补齐即可. \(2.\)每个环长的到环距离必须是连续的. \(3.\)必须要有某个环去接受环长为\(?\)的距离最长的点. 那么我们可以枚举把环长为\(?\)的距离最长的点放在哪个长度的环,然后二分图匹配补齐\(1,2\)两条限制,多余出的点随便搞搞就完事了. #include<cstdio> #include<vect