2016北京网络赛E hihocode1387 A Research on "The Hundred Family Surnames"(LCA+树直径)
2016-09-25 14:39
387 查看
思路:参照ICPC-CAMP的题解
题意: 给一棵树,每个节点上有个颜色,很多询问,询问两种颜色,问从这两种颜色中各取一个节点,距离最大是多少。
题解:处理出每种颜色的节点们的直径(也就是距离最大的点对)。然后对于两种询问颜色
tips:证明考虑反证法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int maxm = 19;
const int maxn = 1e5+7;
struct Edge
{
int v,nxt;
}edge[maxn*20];
int head[maxn];
int tot = 0;
int lca[maxn][maxm],a[maxn],dep[maxn];
int dp[maxn][2]; //结点id的最左端点和最右端点
int n,q;
void add_edge(int u,int v)
{
edge[tot].v=v;
edge[tot].nxt = head[u];
head[u]=tot++;
}
void dfs(int u,int f)
{
for(int i = head[u];~i;i=edge[i].nxt)
{
int v = edge[i].v;
if(v==f)continue;
dep[v]=dep[u]+1;
lca[v][0]=u;
for(int j = 1;j<maxm;j++)
{
int fa = lca[v][j-1];
if(fa==-1)continue;
lca[v][j]=lca[fa][j-1];
}
dfs(v,u);
}
}
int up(int u,int d)
{
for(int i = maxm-1;i>=0;i--)
{
if(d<(1<<i))continue;
u=lca[u][i];
d-=(1<<i);
}
return u;
}
int Lca(int x,int y)
{
if(dep[x]>dep[y])swap(x,y);
y=up(y,dep[y]-dep[x]);
if(x==y)return x;
for(int i = maxm-1;i>=0;i--)
{
if(lca[x][i]!=lca[y][i])
x=lca[x][i],y=lca[y][i];
}
return lca[x][0];
}
int getdis(int u,int v)
{
int root = Lca(u,v);
return dep[u]+dep[v]-2*dep[root]+1;
}
void work(int u,int id)
{
if(dp[id][0]==-1)
{
dp[id][0]=u;
dp[id][1]=u;
return;
}
int dis1 = getdis(u,dp[id][0]);
int dis2 = getdis(u,dp[id][1]);
int dis3 = getdis(dp[id][0],dp[id][1]);
if(dis1>=dis2) //当前u偏向右边
{
if(dis1>dis3)
dp[id][1]=u;
}
else if (dis2>dis3)
dp[id][0]=u;
}
void dfs1(int u,int f)
{
work(u,a[u]);
for(int i = head[u];~i;i=edge[i].nxt)
{
int v = edge[i].v;
if(v==f)continue;
dfs1(v,u);
}
}
map<string,int>vis;
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(dp,-1,sizeof(dp));
memset(lca,-1,sizeof(lca));
memset(head,-1,sizeof(head));
tot = 0;
vis.clear();
string tmp;
int cnt = 1;
for(int i = 1;i<=n;i++)
{
cin >> tmp;
if(!vis.count(tmp))
{
vis[tmp]=cnt;
a[i]=cnt++;
}
else a[i]=vis[tmp];
}
for(int i = 1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,-1); //处理lca
dfs1(1,-1);
for(int i = 1;i<=q;i++)
{
string u,v;
cin >> u >> v;
if(!vis.count(u)|| !vis.count(v))
{
printf("-1\n");
continue;
}
int st = vis[u];
int ed = vis[v];
int dis1 = getdis(dp[st][0],dp[ed][0]);
int dis2 = getdis(dp[st][0],dp[ed][1]);
int dis3 = getdis(dp[st][1],dp[ed][0]);
int dis4 = getdis(dp[st][1],dp[ed][1]);
printf("%d\n",max(max(dis1,dis2),max(dis3,dis4)));
}
}
}
时间限制:3000ms
单点时限:3000ms
内存限制:256MB
The Hundred Family Surnames is a classic Chinese text composed of common Chinese surnames. The book was composed in the early Song Dynasty. It originally contained 411 surnames, and was later expanded to 504. Of
these, 444 are single-character surnames, and 60 are double-character surnames. (quoted from Wikipedia)
Li Lei, a student of Peking University, has done some research on that name book. He wrote a program to built a surname tree, as implied by the book. Here, "tree" is a concept of graph theory. On Li's surname tree,
each node is a Chinese surname. A Chinese surname consists of only English letters and its length is no more than 5 letters.
There is a mysterious legend about this surname tree. If a pair of Chinese loves can find a path on the tree whose two end points are their surnames, they will have a happy marriage. The longer the path is , the
more happiness they will have.
Now, we have many pairs of lovers, they want to find out the longest path whose two end points are their surnames.
The input contains no more than 10 test cases.
For each case, the first line contains two positive integers N and Q(N,Q <= 105). N is the number of nodes
on the tree which numbered from 1 to N, and Q is the number of queries.
Among the following N lines, the i-th line is the surname of node i .
In the next N-1 lines, each line contains two numbers x , y , meaning that there is an edge between node x and node y.
Then, each of the next Q lines is a query, which contains two strings meaning the surnames of two lovers.
For every query , output the number of nodes on the longest happiness path. If the path does not exist, output -1.
样例输入
样例输出
EmacsNormalVim
题意: 给一棵树,每个节点上有个颜色,很多询问,询问两种颜色,问从这两种颜色中各取一个节点,距离最大是多少。
题解:处理出每种颜色的节点们的直径(也就是距离最大的点对)。然后对于两种询问颜色
(a,b)(a,b)的直径
(au,av)(au,av)和
(bu,bv)(bu,bv),答案就是
\max\{dis(au,bu),dis(au,bv),dis(av,bu),dis(av,bv)\}max{dis(au,bu),dis(au,bv),dis(av,bu),dis(av,bv)}。
O(n \log n)O(nlogn)算一种颜色的节点们的直径的方法:增量添加节点同时维护直径,假设新加的节点是
zz,原直径是
(x,y)(x,y),那么新直径是
(x,y),(x,z),(y,z)(x,y),(x,z),(y,z)中的一个。
tips:证明考虑反证法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int maxm = 19;
const int maxn = 1e5+7;
struct Edge
{
int v,nxt;
}edge[maxn*20];
int head[maxn];
int tot = 0;
int lca[maxn][maxm],a[maxn],dep[maxn];
int dp[maxn][2]; //结点id的最左端点和最右端点
int n,q;
void add_edge(int u,int v)
{
edge[tot].v=v;
edge[tot].nxt = head[u];
head[u]=tot++;
}
void dfs(int u,int f)
{
for(int i = head[u];~i;i=edge[i].nxt)
{
int v = edge[i].v;
if(v==f)continue;
dep[v]=dep[u]+1;
lca[v][0]=u;
for(int j = 1;j<maxm;j++)
{
int fa = lca[v][j-1];
if(fa==-1)continue;
lca[v][j]=lca[fa][j-1];
}
dfs(v,u);
}
}
int up(int u,int d)
{
for(int i = maxm-1;i>=0;i--)
{
if(d<(1<<i))continue;
u=lca[u][i];
d-=(1<<i);
}
return u;
}
int Lca(int x,int y)
{
if(dep[x]>dep[y])swap(x,y);
y=up(y,dep[y]-dep[x]);
if(x==y)return x;
for(int i = maxm-1;i>=0;i--)
{
if(lca[x][i]!=lca[y][i])
x=lca[x][i],y=lca[y][i];
}
return lca[x][0];
}
int getdis(int u,int v)
{
int root = Lca(u,v);
return dep[u]+dep[v]-2*dep[root]+1;
}
void work(int u,int id)
{
if(dp[id][0]==-1)
{
dp[id][0]=u;
dp[id][1]=u;
return;
}
int dis1 = getdis(u,dp[id][0]);
int dis2 = getdis(u,dp[id][1]);
int dis3 = getdis(dp[id][0],dp[id][1]);
if(dis1>=dis2) //当前u偏向右边
{
if(dis1>dis3)
dp[id][1]=u;
}
else if (dis2>dis3)
dp[id][0]=u;
}
void dfs1(int u,int f)
{
work(u,a[u]);
for(int i = head[u];~i;i=edge[i].nxt)
{
int v = edge[i].v;
if(v==f)continue;
dfs1(v,u);
}
}
map<string,int>vis;
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(dp,-1,sizeof(dp));
memset(lca,-1,sizeof(lca));
memset(head,-1,sizeof(head));
tot = 0;
vis.clear();
string tmp;
int cnt = 1;
for(int i = 1;i<=n;i++)
{
cin >> tmp;
if(!vis.count(tmp))
{
vis[tmp]=cnt;
a[i]=cnt++;
}
else a[i]=vis[tmp];
}
for(int i = 1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,-1); //处理lca
dfs1(1,-1);
for(int i = 1;i<=q;i++)
{
string u,v;
cin >> u >> v;
if(!vis.count(u)|| !vis.count(v))
{
printf("-1\n");
continue;
}
int st = vis[u];
int ed = vis[v];
int dis1 = getdis(dp[st][0],dp[ed][0]);
int dis2 = getdis(dp[st][0],dp[ed][1]);
int dis3 = getdis(dp[st][1],dp[ed][0]);
int dis4 = getdis(dp[st][1],dp[ed][1]);
printf("%d\n",max(max(dis1,dis2),max(dis3,dis4)));
}
}
}
时间限制:3000ms
单点时限:3000ms
内存限制:256MB
描述
The Hundred Family Surnames is a classic Chinese text composed of common Chinese surnames. The book was composed in the early Song Dynasty. It originally contained 411 surnames, and was later expanded to 504. Ofthese, 444 are single-character surnames, and 60 are double-character surnames. (quoted from Wikipedia)
Li Lei, a student of Peking University, has done some research on that name book. He wrote a program to built a surname tree, as implied by the book. Here, "tree" is a concept of graph theory. On Li's surname tree,
each node is a Chinese surname. A Chinese surname consists of only English letters and its length is no more than 5 letters.
There is a mysterious legend about this surname tree. If a pair of Chinese loves can find a path on the tree whose two end points are their surnames, they will have a happy marriage. The longer the path is , the
more happiness they will have.
Now, we have many pairs of lovers, they want to find out the longest path whose two end points are their surnames.
输入
The input contains no more than 10 test cases.For each case, the first line contains two positive integers N and Q(N,Q <= 105). N is the number of nodes
on the tree which numbered from 1 to N, and Q is the number of queries.
Among the following N lines, the i-th line is the surname of node i .
In the next N-1 lines, each line contains two numbers x , y , meaning that there is an edge between node x and node y.
Then, each of the next Q lines is a query, which contains two strings meaning the surnames of two lovers.
输出
For every query , output the number of nodes on the longest happiness path. If the path does not exist, output -1.样例输入
3 3 Chen Qian Zhuge 1 2 2 3 Chen Chen Chen Sun Zhuge Chen 4 2 Chen Chen Qian Qian 1 2 2 3 1 4 Chen Qian Qian Qian
样例输出
1 -1 3 3 4
EmacsNormalVim
相关文章推荐
- hihoCoder 1387 A Research on "The Hundred Family Surnames" 2016 ICPC 北京网络赛E题
- 2016icpc北京网络赛 E A Research on "The Hundred Family Surnames" 【Lca+树的直径】
- hihocoder 1387 A Research on The Hundred Family Surnames
- how to fix the problem when you install matlab on *nix-- "libXp.so.6 : cannot open "
- [旧稿]How To Add Simple "Install WebPart" Menu to the Right Click On a CAB File
- Handbook of Research on Global Information Technology Management in the Digital Economy
- No such thing as "deleted" on the Internet
- Congratulations on the post " face search " getting google ranking top1 !
- The world is built on C++"
- notes on ibm:dw"5 things you didn't know about ... Command-line flags for the JVM"
- solve the error "Errno 5 Input/Output error" on linux system
- On the error "Type universe cannot resolve assembly" ( in VS2010 using WeifenLuo.WinFormsUI.Docking.dll)
- Paper Research on the scheme of spatial database building based on the integration of GIS and CAD finished
- (C#) A demo on how to auto fill out the "account" value in log in web page in "WebBrowser" control.
- omap35 出现 No more output is seen on the console after "Starting Kernel..." 调试信息
- Fixing "The following VMware kernal modules have been found on your system that were not installed by the VMware installer."
- Research on the Oracle 11g
- Why is the "DEBUG: Error 2836" message generated in the DB2 V8, V9.1, or V9.5 installation log file on Windows system?
- Research on Computer Aided Design Method for the particles distribution of Table-tennis racket
- QTP破解失败:Failed to add license code "UNKNOWN" to the license server on host "no-net".