2015弱校联盟(2) - J. Usoperanto
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的更多相关文章
- 2015弱校联盟(1) -J. Right turn
J. Right turn Time Limit: 1000ms Memory Limit: 65536KB frog is trapped in a maze. The maze is infini ...
- 2015弱校联盟(1) - C. Censor
C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...
- 2015弱校联盟(1) - B. Carries
B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- 2015弱校联盟(1) -A. Easy Math
A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...
- 2015弱校联盟(1) - E. Rectangle
E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...
- (2016弱校联盟十一专场10.3) B.Help the Princess!
题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...
- (2016弱校联盟十一专场10.3) A.Best Matched Pair
题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...
- 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 ...
随机推荐
- Jquery 捕捉页面关闭事件
(http://www.php100.com/html/program/jquery/2013/0905/6052.html) $(window).bind('beforeunload',functi ...
- Webform购物车(用Session存储,页面传值)
购物车主要实现的功能: ①在主页面可以将所有商品显示出来,包括价格,库存. ②点击购买可以累加产品,如果是同一种产品,只会累加每种产品的数量. ③查看购物车,可以查看明细,包括所购物品的名称,价格,数 ...
- 集中式版本控制VS分布式版本控制
CVS及SVN都是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢? 集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所 ...
- Oracle TDE的数据加密示例并用logminer验证加密效果
1.确认数据库版本 2创建密钥钱包 3创建加密列的表并初始值 4演示TDE的数据加密示例 5 logminer验证加密效果
- Oracle登录操作系统验证和密码文件验证
1.确认数据库版本 2.查看当前配置文件 ORALCE数据库不同的登录验证方式是和SQLNET.ORA配置文件有关系的,在配置文件中有一个参数sqlnet.authentication_service ...
- rman恢复误删除的一张表(不完全恢复)
恢复误删除的一张表可以使用很多方法,如日志挖掘.闪回等,rman恢复(不完全恢复)肯定不是最好的,也不建议用, 现在我们只是演示一下这种恢复. 1 RMAN备份数据库 2创建测试表 3查看此时的SCN ...
- RDS MySQL 连接数满情况的处理
RDS MySQL 连接数满情况的处理 RDS MySQL 连接数满有2种情况 1. 空闲连接过多 原因: 应用使用长连接模式 - 对于长连接模式(比如Java应用),应用侧应该配置连接池.连接池的初 ...
- 经验分享:Xcode 创建.a和framework静态库【转】
作者:Haley_Wong 最近因为项目中的聊天SDK,需要封装成静态库,所以实践了一下创建静态库的步骤,做下记录. 库介绍 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态 ...
- Linux系统下Redis安装(一)
最近项目要使用Redis,特将这段时间将Redis的学习经验与大家分享,算是对这段时间学习成果的总结和技术提炼,不足之处还望大家批评指正. 项目背景: 有些很少改动的数据和经常使用的数据(例如系统中下 ...
- php命名、注释规范
一.注释 1.文件头部模板 /** *这是一个什么文件 * *此文件程序用来做什么的(详细说明,可选.). * @author richard<e421083458@163.com> * ...