A;
【思路】
flag标记起点0 是否涵盖, IN控制能到达的最远距离
int main()
{
int n,m;
int a,b;
scanf("%d %d",&n,&m);int flag=1;
int sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&a,&b);
if(a==0&&b>=sum)
{
flag=0;
sum=b;
}
if(b>=sum&&a<=sum)
sum=b;
}
if(flag)
cout<<"NO"<<endl;
else
{
if(sum>=m)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
B:
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.
Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0.
You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.
It is guaranteed that you have to color each vertex in a color different from 0.
You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).
The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi.
The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into.
It is guaranteed that the given graph is a tree.
Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.
6 1 2 2 1 5 2 1 1 1 1 1
3
7 1 1 2 3 1 4 3 3 1 1 1 2 3
5
The tree from the first sample is shown on the picture (numbers are vetices' indices):
On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):
On seond step we color all vertices in the subtree of vertex 5 into color 1:
On third step we color all vertices in the subtree of vertex 2 into color 1:
The tree from the second sample is shown on the picture (numbers are vetices' indices):
On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):
On second step we color all vertices in the subtree of vertex 3 into color 1:
On third step we color all vertices in the subtree of vertex 6 into color 2:
On fourth step we color all vertices in the subtree of vertex 4 into color 1:
On fith step we color all vertices in the subtree of vertex 7 into color 3:
【思路】
循环节,找树的分支,从底部扫描到顶部,如果节点与父节点不同ans++
int a[MAXN];
int b[MAXN],vis[MAXN];
int main()
{
int n;
map<int,int>mp;
scanf("%d",&n);
mem(vis,0);
for(int i=2;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
int ans=1;
for(int i=n;i>=2;i--)
{
if(!vis[i])
{
int pre=i;
vis[pre]=1;
while(pre!=1)
{
if(b[pre]!=b[a[pre]]&&!mp[pre])
{
ans++;
// cout<<pre<<endl;
mp[pre]=pre;
}
pre=a[pre];
vis[pre]=1;
}
}
}
cout<<ans<<endl;
return 0;
}
C;
Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a0, a1, ..., ah, where his the height of the tree, and ai equals to the number of vertices that are at distance of i edges from root.
Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence ai, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.
Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.
The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.
The first line contains a single integer h (2 ≤ h ≤ 105) — the height of the tree.
The second line contains h + 1 integers — the sequence a0, a1, ..., ah (1 ≤ ai ≤ 2·105). The sum of all ai does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence.
If there is only one tree matching this sequence, print "perfect".
Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print integers, the k-th of them should be the parent of vertex k or be equal to zero, if the k-th vertex is the root.
These treese should be non-isomorphic and should match the given sequence.
2 1 1 1
perfect
2 1 2 2
ambiguous 0 1 1 3 3 0 1 1 3 2
The only tree in the first example and the two printed trees from the second example are shown on the picture:
【思路】
特例:
int a[MAXN];
int b[maxn][2];
int main()
{
int h;
scanf("%d",&h);
int flag=1;
int sum=0;
for(int i=1;i<=h+1;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
int cont=0;
for(int i=1;i<=h+1;i++)
{
if(a[i]>=2)
cont++;
else
cont=0;
if(cont>=2)
{
flag=0;
break;
}
}
if(flag)
{
cout<<"perfect"<<endl;
return 0;
}
cout<<"ambiguous"<<endl;
mem(b,0);
int k=0;
for(int i=1;i<=h+1;i++)
{
for(int j=1;j<=a[i];j++)
{
cout<<k<<" ";
}
k+=a[i];
}
cout<<endl;
k=0;
int flag2=1,fflag;
for(int i=1;i<=h+1;i++)
{
for(int j=1;j<=a[i];j++)
{
if(a[i-1]>=2&&flag2&&a[i]>=2)
{
cout<<k-1<<" ";
flag2=0;
}
else
{
cout<<k<<" ";
}
}
k+=a[i];
}
cout<<endl;
return 0;
}