J. Usoperanto

Time Limit: 8000ms

Memory Limit: 256000KB

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 adjective beta (“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 - 1, Li ≠ 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 1

3

3 -1

4 0

5 0

Output for the Sample Input 1

4

Sample Input 2

3

10 -1

10 0

10 1

Output for the Sample Input 2

0

Sample Input 3

4

1 -1

1 0

1 1

1 0

Output for the Sample Input 3

1

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int Max = 1e6+100;
vector<int>E[Max];
int Len[Max];
int to[Max];
int num[Max],n;
LL ans;
bool cmp(int a,int b)
{
return Len[a]<Len[b];
}
void bfs()
{
queue<int>Q;
memset(num,0,sizeof(num));
for(int i=0;i<n;i++)
{
if(!E[i].size())//将没有被修饰词放进队列
{
Q.push(i);
}
}
while(!Q.empty())
{
int u=Q.front();
Q.pop();
sort(E[u].begin(),E[u].end(),cmp);//将修饰词按长度从小到大排序,只有这样才能保证花费是最小的.如果修饰词也被修饰,则将他们看做一个整体,长度为他们的总长度.
int len=E[u].size();
for(int i=0,j=len-1;i<len;i++,j--)
{
Len[u]+=Len[E[u][i]];
ans+=(Len[E[u][i]])*j;
}
if(to[u]!=-1)
{
num[to[u]]++;
if(num[to[u]]==E[to[u]].size())//如果被修饰词的所有的修饰词计算完毕,就入队列
{
Q.push(to[u]);
}
}
}
}
int main()
{
int b;
while(~scanf("%d",&n))
{
memset(E,0,sizeof(E));
for(int i=0;i<n;i++)
{
scanf("%d %d",&Len[i],&b);
to[i]=b;//记录自己所修饰的词的位置
if(b!=-1)
{
E[b].push_back(i);//建立边
}
}
ans=0;
bfs();
cout<<ans<<endl;
}
return 0;
}

2015弱校联盟(2) - J. Usoperanto的更多相关文章

  1. 2015弱校联盟(1) -J. Right turn

    J. Right turn Time Limit: 1000ms Memory Limit: 65536KB frog is trapped in a maze. The maze is infini ...

  2. 2015弱校联盟(1) - C. Censor

    C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...

  3. 2015弱校联盟(1) - B. Carries

    B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...

  4. 2015弱校联盟(1) - I. Travel

    I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...

  5. 2015弱校联盟(1) -A. Easy Math

    A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...

  6. 2015弱校联盟(1) - E. Rectangle

    E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...

  7. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  8. (2016弱校联盟十一专场10.3) A.Best Matched Pair

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...

  9. 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...

随机推荐

  1. spring security为不同用户显示各自的登录成功页面

    一个常见的需求是,普通用户登录之后显示普通用户的工作台,管理员登陆之后显示后台管理页面.这个功能可以使用taglib解决. 其实只要在登录成功后的jsp页面中使用taglib判断当前用户拥有的权限进行 ...

  2. 满足要求的最长上升子序列(nlogn)

    题意:数列A1,A2,...,AN,修改最少的数字,使得数列严格单调递增.(1<=N<=10^5; 1<=Ai<=10^9 ) 思路:首先要明白的一点是数列是严格单调递增,那么 ...

  3. SRM 588 DIV1

    250 题意:有n首不同的曲子,你唱每首曲子需要花费a的时间以及一个调整的时间b,调整的时间为此首歌的曲调减去上一首歌的曲调的绝对值. 思路:我们用dp[i][k]表示前i首歌只唱k首用的最小时间花费 ...

  4. MySQL Cluster 配置详细介绍

    在上篇文章已经详细说明了MySQL Cluster搭建与测试,现在来说说详细的配置参数.在MySQL Cluster 环境的配置文件 config.ini 里面,每一类节点都有两个(或以上)的相应配置 ...

  5. 20145337 《Java程序设计》第九周学习总结

    20145337 <Java程序设计>第九周学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC可以 ...

  6. 一些常用的NLTK频率分布类中定义的函数

    fdist=FreqDist(samples)创建包含给定样本的频率分布fist.inc(sample)增加样本fdist['monstrous']计数给定样本出现的次数fdist.freq('mon ...

  7. bash 中的变量

    bash 中的变量 Linux command line 笔记 变量无需声明,自动创建 var=abc #变量a赋值为字符串abc var="hello world" #字符串里有 ...

  8. Excel 中 Index 和 Match 方法的使用

    MATCH函数(返回指定内容所在的位置) MATCH(lookup-value,lookup-array,match-type) lookup-value:表示要在区域或数组中查找的值,可以是直接输入 ...

  9. mysql重点--索引

    1.关于索引 # 什么是索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据. #索引的作用 加速查询和约束. # 为什么索引查询会变快 没创建一个索引会相应的创建一个 ...

  10. jqGrid预定义的格式化类型formatter

    下表列出了jqGrid中的预定义格式化类型 所有预定义类型和编辑模式兼容,就是说数字,链接和email等需要转换,才能使他们被正确编辑 类型 选项(默认值参考语言选项) 描述 integer thou ...