Usoperanto

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://www.bnuoj.com/v3/contest_show.php?cid=6866#problem/J

Description

Usoperanto is an artificial spoken language designed and regulated by Usoperanto Academy. The academy is now in study to establish Strict Usoperanto, a variation of the language intended for formal documents.

In Usoperanto, each word can modify at most one other word, and modifiers are always put before modifiees. For example, with a noun uso ("truth") modified by an adjective makka ("total"), people say makka uso, not uso makka. On the other hand, there have been no rules about the order among multiple words modifying the same word, so in case uso is modified by one more adjectivebeta ("obvious"), people could say both makka beta uso and beta makka uso.

In Strict Usoperanto, the word order will be restricted according to modification costs. Words in a phrase must be arranged so that the total modification cost is minimized. Each pair of a modifier and a modifiee is assigned a cost equal to the number of letters between the two words; the total modification cost is the sum of the costs over all modifier-modifiee pairs in the phrase. For example, the pair of makka and uso in a phrase makka beta uso has the cost of 4 for beta (four letters). As the pair of beta and uso has no words in between and thus the cost of zero, makka beta uso has the total modification cost of 4. Similarly beta makka uso has the total modification cost of 5. Applying the "minimum total modification cost" rule, makka beta uso is preferred to beta makka uso in Strict Usoperanto.

Your mission in this problem is to write a program that, given a set of words in a phrase, finds the correct word order in Strict Usoperanto and reports the total modification cost.

Input

The format of the input is as follows.

N
M0 L0
...
MN-1 LN-1

The first line contains an integer N (1 ≤ N ≤ 106). N is the number of words in a phrase.

Each of the following N lines contains two integers Mi (1 ≤ Mi ≤ 10) and Li (-1 ≤ Li ≤ N - 1Li ≠ i) describing the i-th word (0 ≤ i ≤ N-1). Mi is the number of the letters in the word. Li specifies the modification: Li = -1 indicates it does not modify any word; otherwise it modifies the Li-th word.

Note the first sample input below can be interpreted as the uso-beta-makka case.

Output

Print the total modification cost.

Sample Input

3
3 -1
4 0
5 0

Sample Output

4

HINT

题意

让你用题中给东西,构成一个链

输入是 ai,bi

表示ai必须要放在第bi个单词前面

代价是ai和bi之间的单词长度和

然后问你最小代价是多少

题解:

贪心就好了

这其实是一个图论题,你连接起来,就会构成一棵树

我们就每次贪心的选择最小的点权,且度数为1的点放在第一个,然后依次放

类似拓扑排序的一样做

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1200500
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int n;
int fa[maxn],ind[maxn];
ll sum[maxn];
int lst[maxn],top=;
int pre[maxn],nxt[maxn],to[maxn],cnt; void makeedge(int x,int y)
{
to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
} int main()
{
memset(pre,-,sizeof(pre));
scanf("%d",&n);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
fa[i]=y;
if(y>=)
{
ind[y]++;
makeedge(y,i);
}
sum[i]=x;
}
queue<int> q;
while(!q.empty()) q.pop();
for(int i=;i<n;i++)
if(!ind[i]) q.push(i);
ll ans=,tmp=;
while(!q.empty())
{
int s=q.front();
q.pop();
top=;
for(int p=pre[s];p!=-;p=nxt[p])
{
lst[++top]=sum[to[p]];
}
sort(lst+,lst++top);
tmp=;
for(int i=;i<top;i++)
{
tmp+=lst[i];
ans+=tmp;
}
if(fa[s]>=)
{
--ind[fa[s]];
sum[fa[s]]+=sum[s];
if(!ind[fa[s]])
q.push(fa[s]);
}
}
cout<<ans<<endl;
}

Aizu 2456 Usoperanto 贪心 拓扑排序的更多相关文章

  1. [POI2004] SZP (贪心+拓扑排序)

    [问题描述] Byteotian 中央情报局(BIA) 雇佣了许多特工. 他们每个人的工作就是监视 另一名特工. Byteasar 国王需要进行一次秘密行动,所以他要挑选尽量多的信得过的特工. 但 是 ...

  2. 贪心+拓扑排序 AOJ 2456 Usoperanto

    题目传送门 题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小 分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这 ...

  3. Aizu 2456 Usoperanto (贪心)

    贪心,对于一个修饰关系可以连一条有向边,在合并的时候,子节点的序列一定是连续安排的,因为如果有交叉,交换以后一定更优. 然后一个序列一个序列的考虑,长度短的应该在前面,否则同样交换以后更优.因此排序以 ...

  4. hdu-5695 Gym Class(贪心+拓扑排序)

    题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  5. 2016"百度之星" - 初赛(Astar Round2A)1006 Gym Class(HDU5695)——贪心+拓扑排序

    分析:首先,利用贪心可知,如果要所有人的分数和最高,需要把序号大的优先放在前面.其次,对于a的前面不能为b,那么只能a在b前面了,那么就建立一条从a到b的边,并且b的入度加1.然后就是拓扑排序了.要分 ...

  6. poj 3687 Labeling Balls - 贪心 - 拓扑排序

    Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...

  7. Berland Army CodeForces - 883B (贪心,拓扑排序)

    大意: n个点, 点$i$的等级为$r_i$, 只给出部分点的$r$值, $r_i$的范围为[1,k], 且[1,k]都至少有一个. 给定m条有向边, (x,y)表示$r[x]>r[y]$, 求 ...

  8. Luogu P5603 小C与桌游【贪心+拓扑排序】

    [Description]https://www.luogu.com.cn/problem/P5603 \(\;\) 题意可以简化为:一个不保证联通,n个点,m条边的DAG(有向无环图),构造一个拓扑 ...

  9. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

随机推荐

  1. 如何将DataTable转换成List<T>呢?

    昨日在工作中,遇到一个问题:需要将查询出来的DataTable数据源,转换成List<T>的泛型集合(已知T类型).第一反应,我想肯定要用到“泛型”(这不是废话吗?都说了要转换成List& ...

  2. Logminer实战

    相信许多DBA同学都曾遇到过这种情况,由于各种各样的原因,必须对数据库做不完全恢复:但又不确定应该恢复到哪个时间点或SCN才合适,因而反复的执行不完全恢复,悲剧掉了一地,中枪的有木有?温馨提示下,在生 ...

  3. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  4. 【转】用Pthread创建线程的一个简单Demo

    一.我们直接在COCOS2D-X自带的HelloWorld工程中添加代码.首先将Pthread的文件包含进来包括lib文件.在HelloWorld.cpp中引入头文件和库. #include &quo ...

  5. Android开发优化宝典

    I. 网络相关 http头信息带Cache-Control域 确定缓存过期时间 防止重复请求 直接用IP直连,不用域名,策略性跟新本地IP列表. – DNS解析过程耗时在百毫秒左右,并且还有可能存在D ...

  6. a标签中的label在IE下触发不了a标签的href链接(label标签——解析)

    <a href="http://www.baidu.com/" target="_blank"> <span>百度</span&g ...

  7. 【开源专访】Sea.js创始人玉伯的前端开发之路

    摘要:玉伯,淘宝前端类库 KISSY.前端模块化开发框架SeaJS.前端基础类库Arale的创始人.本期[开源专访]我们邀请玉伯来为我们分享一些关于前端框架.前端开发的那些事,以及前端大牛是如何炼成的 ...

  8. 新手!SDK Manager里找不到API安装的选项怎么办?

    只有Tools和EXTRAS文件夹的选项,没有API包安装,咋办呢?   回复讨论(解决方案) 网络有问题吗? 网络有问题吗? 就是不知道啊 你是在eclispe里面打开的?还是在外面直接打开的?没有 ...

  9. NGINX(三)HASH表

    前言 nginx的hash表有几种不同的种类, 不过都是以ngx_hash_t为基础的, ngx_hash_t是最普通的hash表, 冲突采用的是链地址法, 不过这里冲突的元素不是一个链表, 而是一个 ...

  10. MFC消息映射机制

    1.MFC应用框架主要类之间的关系 MFC自动生成的框架中重要的类有:C-App.CMainFrame.C-Doc和C-View. 其他的类如CClassView.CFileView等都是在框架窗口( ...