关键词:
A. Timofey and a tree
题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样。$N,C le 10^5$
想法:这个叫换根吧。先求出一个点合法即其儿子的子树内颜色一样,非该点子树的点颜色都一样。可以用DFS序解决。
#include< cstdio > typedef long long ll; template inline void read(T&x) { x=0;bool f=0;char c=getchar(); while((c<‘0‘||c>‘9‘)&&c!=‘-‘) c=getchar();if(c==‘-‘)f=1, c=getchar(); while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} x=f?-x:x; } const int MAXN(100010); struct Node{int nd,nx;}bot[MAXN<<1];int tot,first[MAXN]; void add(int a,int b){bot[++tot]=(Node){b,first[a]};first[a]=tot;} int n,a,b,col[MAXN],Eu[MAXN],size[MAXN],num[MAXN],last[MAXN],cnt,Ans; bool dif[MAXN]; void DFS(int x,int f) { Eu[x]=++cnt; num[cnt]=x;size[x]=1; for(int v=first[x];v;v=bot[v].nx) if(bot[v].nd!=f) { DFS(bot[v].nd,x); size[x]+=size[bot[v].nd]; dif[x]|=dif[bot[v].nd]|(col[x]!=col[bot[v].nd]); } } void DP(int x,int f) { bool bf=false; for(int v=first[x];v;v=bot[v].nx) if(bot[v].nd!=f) { DP(bot[v].nd,x); bf|=dif[bot[v].nd]; } if(!bf) { // fprintf(stderr,"%d ",x); int L=Eu[x]-1,R=Eu[x]+size[x]; // fprintf(stderr,"%d %d ",L,R); if((!L||last[1]>=L)&&(R>n||last[R]>=n)&&(!L||R>n||col[num[1]]==col[num[n]]))Ans=x; } } int main() { // freopen("C.in","r",stdin); read(n); for(int i=1;i<n;i++) { read(a);read(b); add(a,b);add(b,a); } for(int i=1;i<=n;i++)read(col[i]); DFS(1,0); for(int i=n;i>=1;i--)last[i]=(col[num[i]]==col[num[i+1]])?last[i+1]:i; DP(1,0); if(!Ans)printf("NO"); else printf("YES %d ",Ans); return 0; }
codeforces763a.timofeyandatree
A.Timofeyandatree题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样。$N,Cle10^5$想法:这个叫换根吧。先求出一个点合法即其儿子的子树内颜色一样,非该点子树的点颜色都一样。可以用DFS序解决。&n... 查看详情
cf763atimofeyandatree(思维题)
题目链接:http://codeforces.com/problemset/problem/763/A题目:EachNewYearTimofeyandhisfriendscutdownatreeof n verticesandbringithome.Afterthattheypaintallthe n itsvertices,sothatthe i 查看详情
codeforcesround#763(div.2)editorialc解题报告(代码片段)
链接:https://codeforces.com/contest/1623/problem/C题解:二分答案即可。比较困扰我的是两个问题:1.怎么求出ddd。2.如何判断这个midmidmid的值是否合法。会不会出现,满足check,但是实际上不存在midmidmid的情况。第一个:intd&... 查看详情
763.partitionlabels分区标签
Astring S oflowercaselettersisgiven.Wewanttopartitionthisstringintoasmanypartsaspossiblesothateachletterappearsinatmostonepart,andreturnalistofintegersrepresentingthesizeoftheseparts.Example 查看详情
763.partitionlabels(代码片段)
题目描述:Astring S oflowercaselettersisgiven.Wewanttopartitionthisstringintoasmanypartsaspossiblesothateachletterappearsinatmostonepart,andreturnalistofintegersrepresentingthesizeoftheseparts.&n 查看详情
763.partitionlabels(贪心)(代码片段)
AstringSoflowercaselettersisgiven.Wewanttopartitionthisstringintoasmanypartsaspossiblesothateachletterappearsinatmostonepart,andreturnalistofintegersrepresentingthesizeoftheseparts.Example1:Input:S="a 查看详情
c_cpp763.cpp(代码片段)
codeforcesround#763(div.2)editorialc解题报告(代码片段)
链接:https://codeforces.com/contest/1623/problem/C题解:二分答案即可。比较困扰我的是两个问题:1.怎么求出ddd。2.如何判断这个midmidmid的值是否合法。会不会出现,满足check,但是实际上不存在midmidmid的情况。第一个:intd&... 查看详情
763.划分字母区间(代码片段)
方法一:滑动窗口classSolutionpublicList<Integer>partitionLabels(Strings)List<Integer>res=newArrayList<>();int[]arr=newint[128];for(charc:s.toCharArray())arr[c]++;int[]window=newint 查看详情
*leetcode763.partitionlabels(代码片段)
https://leetcode.com/problems/partition-labels/description/constintSIZE=256;classSolutionpublic:booljudge(intcnt[],inthash[])//boolret=true;for(inti=0;i<SIZE;i++)if(hash[i]= 查看详情
763.划分字母区间(代码片段)
字符串S由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段。返回一个表示每个字符串片段的长度的列表。 示例1:输入:S="ababcbacadefegdehijhklij"输出:[9,7,8]解释:划分结果... 查看详情
763.partitionlabels(代码片段)
Astring S oflowercaselettersisgiven.Wewanttopartitionthisstringintoasmanypartsaspossiblesothateachletterappearsinatmostonepart,andreturnalistofintegersrepresentingthesizeoftheseparts. Example1:Input:S="ababcbacadefegdehijhklij"Output:[9,7,8]Explanation:Thepartitionis"ababcbaca","defeg... 查看详情
cf763b.timofeyandrectangles
%%题解,脑洞好大啊。四色定理什么鬼的。。所以一定是yes。因为矩形边长都是奇数,所以可以按左下角分类,一共4类,分别1,2,3,4就可以了。(需要4种颜色的情况大概就是4个矩形围起来一个矩形)1#include<bits/stdc++.h>2#defineL... 查看详情
763.划分字母区间(代码片段)
目录1、Question2、Analysis3、Code4、Result1、Question 字符串S由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。示例:输入... 查看详情
[m贪心]lc763.划分字母区间(贪心+代码实现)(代码片段)
文章目录1.题目来源2.题目解析1.题目来源链接:763.划分字母区间2.题目解析lc讨论中看见的华为od面试编程题目,可以试试。贪心思路:本次分割字符串如果在后面都不出现,则单独成一段,作为有效答案。因... 查看详情
codeforcesround#763(div.2)d(推式子+概率)
题目大意:给n*m的矩阵,给机器人起点和垃圾的终点,一开始向右下移动,撞到边界时会反方向移动,当机器人与垃圾同行或者同列时有p/100的概率清理垃圾,求机器人清理掉垃圾的步数期望思路:法... 查看详情
力扣算法jslc[435.无重叠区间]lc[763.划分字母区间]
菜鸡刷算法的一天,每天分享两题算法,大家有这个想法的,可以给我个关注,然后一起坚持每天的算法之旅。希望我们共同进步,一起加油。菜鸡刷算法的一天,每天分享两题算法,大家有这个想法的,可以给我个关注,然后... 查看详情
菜鸟级彩友机选揽大奖旺站中大乐透763万
...票大乐透第16098期开奖,吉林长春一彩民喜获一等奖,将763万元奖金纳入囊中。 中得大奖的地址为:长春市西城家园16栋109号门市第02230代销点,当业主庞先生得知本站中了一个一等奖,特别兴奋,随即制作了大喜报及多条条... 查看详情