vis

C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

萝らか妹 提交于 2019-11-28 14:50:44
题面: 传送门 题目描述: 给出有n个节点的树,整数k。题目要求找长度为k,符合规则(good序列)的“点序列”(由节点构成的序列)个数有多少?规则如下: 1.走一条出发点为a1,终点为ak的一条路(允许重复边,重复点) 2.从a1开始,通过最短路径走到a2,然后从a2通过最短路径走到a3,以此类推,直到走到终点 3.如果在上述过程中,至少经过一条“黑边”,则这个序列是good的 题目分析一: 这道题直接分析确实挺难,难在哪里呢?我们看看这个good序列要满足什么条件: 1.走一条路:这里要注意的就是可以重复点,其他没什么可以引起注意的地方 2.从a1走到a2,a2走到a3......如果这个过程经过了黑边,这个序列就是good序列: 所以刚开始我们的想法是: 找一条黑边两端的端点,然后看看包含这两个端点的序列有多少个,再减去重复的。 但是,想法很美好,情况很复杂😭,我刚开始就是这么想的。后面发现越来越不对劲,就重新看了一下题目,发现了一些重要的突破口: 1.题目的good序列是“至少”经过一条黑边,注意,这里的用词是用“至少”。 2.原题目的最后还提醒了:总共有n^k个序列,算其中good序列有多少个。 然后我就想到了:既然good序列这么难算,不如算算bad序列? bad序列规则:第一点和第二点不变,第三点:如果在上述过程中,没有经过一条“黑边”,则这个序列是bad的。也就是说

【刷题】披风

左心房为你撑大大i 提交于 2019-11-28 14:03:18
有向图,每个点有点权,找出途中的所有路径中,路径上最大值和最小值的差值 的最大值 20%,N<=50 40%,N<=100 60%,N<=1000 另外20%,图中无环 100%,N<=100 000,M<=500 000 点权均为int型正整数 60分算法: 当然是练习暴力啦,不过这暴力我wa了好多次, 最高得分60 学到一个方法: 枚举路径,可以直接全部枚举起终点 #include<cstdio> #include<cstdlib> #include<vector> using namespace std; int n,m; const int N=1003; vector <int> g[N]; int d[N],ans; bool vis[N]; void dfs(int u,int ed,int mx,int mn) { if(u==ed) { ans=max(ans,mx-mn); return ; } int sz=g[u].size() ; for(int i=0;i<sz;i++) { int v=g[u][i]; if(vis[v]) continue; vis[v]=true; dfs(v,ed,max(mx,d[v]),min(mn,d[v])); vis[v]=false; } } int main() { scanf("%d%d",&n,&m); for

[USACO07FEB]Lilypad Pond

喜欢而已 提交于 2019-11-28 13:33:57
Link 考场上把我送走的一道题 考试时,晃眼一看,这不是裸的最短路计数吗?!赶忙写好了代码,自信地关闭了文件。然后,0分…… 那么回过头来,到底错在哪里?只有新加的荷叶才会被统计,而原来就有的荷叶不算在方案数里(学长把题目魔改了,并且说的不是很清楚,一直没搞懂样例,然后我就挂了) 那怎么样才能使得不把原来就有的荷叶作为状态算在方案数里?因为原来有的荷叶直接可以跳,不需要花费,所以不把它与它可以跳到的点连边,而是把它作为中转节点,把它可以到达的所有点互相连边,最后转化为最短路计数。 可是就这样过了吗?并没有,还有一种情况没有考虑。就是两个原本就有的荷叶可以相互到达的情况,此时就相当于有两个中转节点,需要把它们可以到达的至多16个点互相连边(此过程可以用bfs解决)。如果是三个及三个以上呢?以此类推罢了。 Code: #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ll long long #define INF 0x3f3f3f3f #define N 100 template<class T> inline void read(T &x){ x=0;char c=getchar();T flag=1; while(c<'0'||c>'9'){if(c=='-'

[最短路,最大流最小割定理] 2019 Multi-University Training Contest 1 Path

人盡茶涼 提交于 2019-11-28 13:08:59
题目: http://acm.hdu.edu.cn/showproblem.php?pid=6582 Path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 3747 Accepted Submission(s): 1075 Problem Description Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her. After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly n houses, and some of them are connected with directed road.

CCF-CSP题解 201604-4 游戏

ε祈祈猫儿з 提交于 2019-11-28 13:04:01
bfs #include <bits/stdc++.h> const int maxn = 100; using namespace std; int n, m, t; bool hasDanger[maxn + 10][maxn + 10]; int danger[maxn + 10][maxn + 10][2]; struct tNode { int r, c; int time; tNode(int rr, int cc, int ttime):r(rr), c(cc), time(ttime){} }; int vis[maxn + 10][maxn + 10][310]; int main() { scanf("%d%d%d", &n, &m, &t); memset(hasDanger, 0, sizeof(hasDanger)); for (int i = 1, r, c, a, b; i <= t; i++) { scanf("%d%d%d%d", &r, &c, &a, &b); hasDanger[r][c] = true; danger[r][c][0] = a; danger[r][c][1] = b; } memset(vis, 0, sizeof(vis)); queue<tNode> q; q.push(tNode(1, 1, 0)); vis[1]

DFS-递归入门

我只是一个虾纸丫 提交于 2019-11-28 12:39:26
【递归入门】 题目描述 已知 n 个整数b1,b2,…,bn 以及一个整数 k(k<n)。 从 n 个整数中任选 k 个整数相加,可分别得到一系列的和。 例如当 n=4,k=3,4 个整数分别为 3,7,12,19 时,可得全部的组合与它们的和为:     3+7+12=22  3+7+19=29  7+12+19=38  3+12+19=34。   现在,要求你计算出和为素数共有多少种。 例如上例,只有一种的和为素数:3+7+19=29。 输入 第一行两个整数:n , k (1<=n<=20,k<n) 第二行n个整数:x1,x2,…,xn (1<=xi<=5000000) 输出 一个整数(满足条件的方案数)。 样例输入 4 3 3 7 12 19 样例输出 1 代码 #include <iostream> #include <cstring> #include <vector> using namespace std; //判断是否是素数 bool isprime(int n) { if (n<1)return false; for (int i=2;i*i<n;i++) { if (n%i==0)//n&(i-1)==0 return false; } return true; } int a[22],p[22], b[22]; bool vis[22]; int n, k,

图(1):最短路径

蹲街弑〆低调 提交于 2019-11-28 12:26:17
Dijkstra算法 1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible. Input Specification: Each input file contains one test case. For each test case, the first line

最小树形图:朱刘算法

强颜欢笑 提交于 2019-11-28 12:22:15
template LUOGU 4716 #include<bits/stdc++.h> const int maxn=1e2+50,maxm=1e4+50,inf=0x3f3f3f3f; namespace IO { char buf[1<<15],*fs,*ft; inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; } template<typename T>inline void read(T &x) { x=0; T f=1, ch=getchar(); while (!isdigit(ch) && ch^'-') ch=getchar(); if (ch=='-') f=-1, ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar(); x*=f; } char Out[1<<24],*fe=Out; inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; } template<typename T>inline void write(T x,char str) { if (!x) *fe++

cf 1174 D Ehab and the Expected XOR Problem

℡╲_俬逩灬. 提交于 2019-11-28 11:20:06
cf 1174 D Ehab and the Expected XOR Problem 题意 在1~ \(2^n\) 范围内找到一个最长的序列,使得该序列的每一个子串异或后不等于0和x 题解 假设该序列为a,那么前缀异或和b[i] = a[i]^a[i-1]^...^a[0],如果b之间异或都不会等于0和x,那么a之间也不会。 #include <cstdio> #include <cstring> int main() { int n, x; while(~scanf("%d %d", &n, &x)) { n = 1 << n; int ans[300000], cnt = 0; bool vis[300000]; memset(vis, false, sizeof(vis)); vis[x] = true; for(int i = 1; i < n; i++) { if(vis[i] == false){ ans[cnt++] = i; vis[i] = vis[i^x] = true; } } printf("%d\n", cnt); if(cnt) printf("%d ", ans[0]); for(int i = 1; i < cnt; i++) { printf("%d ", ans[i] ^ ans[i-1]); } if(cnt) printf("\n"); }

洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解

雨燕双飞 提交于 2019-11-28 11:12:53
题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节。 由于牛棚不太大,FJ通过指定奶牛必须遵循的穿越路线来确保奶牛的乐趣。为了实现这个让奶牛在牛棚里来回穿梭的方案,FJ在第i号隔间上张贴了一个“下一个隔间”Next_i(1<=Next_i<=N),告诉奶牛要去的下一个隔间;这样,为了收集它们的糖果,奶牛就会在牛棚里来回穿梭了。 FJ命令奶牛i应该从i号隔间开始收集糖果。如果一只奶牛回到某一个她已经去过的隔间,她就会停止收集糖果。 在被迫停止收集糖果之前,计算一下每头奶牛要前往的隔间数(包含起点)。 输入格式 第1行 整数n。 第2行到n+1行 每行包含一个整数 next_i 。 输出格式 n行,第i行包含一个整数,表示第i只奶牛要前往的隔间数。 样例解释 有4个隔间 隔间1要求牛到隔间1 隔间2要求牛到隔间3 隔间3要求牛到隔间2 隔间4要求牛到隔间3 牛1,从1号隔间出发,总共访问1个隔间; 牛2,从2号隔间出发,然后到三号隔间,然后到2号隔间,终止,总共访问2个隔间; 牛3,从3号隔间出发,然后到2号隔间,然后到3号隔间,终止,总共访问2个隔间; 牛4,从4号隔间出发,然后到3号隔间,然后到2号隔间,然后到3号隔间,终止,总共访问3个隔间。 翻译提供者:吃葡萄吐糖 题目描述