A - A
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1272
Description
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。
Input
输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。
整个文件以两个-1结尾。
Output
对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出”Yes”,否则输出”No”。
Sample Input
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output
Yes
Yes
No
并查集应用 注意判断输入0 0的时候输出Yes
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int M = 100005;
int a,b;
int father[M];
bool circle;
bool visit[M];
int edgenum,vnum;
void initial( )
{
for( int i=0 ; i<M ; i++ )
father[i] = i,visit[i]=false;
circle = false;
edgenum = vnum = 0;
}
int find( int x )
{
return x == father[x] ? x : father[x] = find(father[x]);
}
void merge( int a,int b )
{
if( a == b )
circle = true;
int x, y;
x = find(a);
y = find(b);
if( x != y )
{
father[x] = y;
edgenum++;
}
else
circle = true;
}
int main()
{
while( true )
{
initial( );
scanf("%d%d",&a,&b);
if( a==0 && b==0 )
{
printf("Yes\n");
continue;
}
if( a==-1 && b==-1 )
break;
visit[a] = true;
visit[b] = true;
merge( a,b );
while( true )
{
scanf("%d%d",&a,&b);
if( a==0 && b==0 )
break;
visit[a] = true;
visit[b] = true;
merge( a, b );
}
for( int i=0 ; i<M ; i++ )
if( visit[i] )
vnum++;
if( !circle && edgenum+1 == vnum )
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
B - B
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
CSU 1660
Description
A simple cycle is a closed simple path, with no other repeated vertices or edges other than the starting and ending vertices. The length of a cycle is the number of vertices on it. Given an undirected graph G(V, E), you are to detect whether it contains a simple cycle of length K. To make the problem easier, we only consider cases with small K here.
Input
There are multiple test cases.
The first line will contain a positive integer T (T ≤ 10) meaning the number of test cases.
For each test case, the first line contains three positive integers N, M and K ( N ≤ 50, M ≤ 500, 3 ≤ K ≤ 7). N is the number of vertices of the graph, M is the number of edges and K is the length of the cycle desired. Next follow M lines, each line contains two integers A and B, describing an undirected edge AB of the graph. Vertices are numbered from 0 to N-1.
Output
For each test case, you should output “YES” in one line if there is a cycle of length K in the given graph, otherwise output “NO”.
Sample Input
2
6 8 4
0 1
1 2
2 0
3 4
4 5
5 3
1 3
2 4
4 4 3
0 1
1 2
2 3
3 0
Sample Output
YES
NO
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
int T,N,M,K;
vector<int> graph[55];
int mark[55];
bool findCycle;
void dfs(int prev,int now,int cnt)
{
if (findCycle) return;
//cout<<prev<<"->"<<now<<" : "<<cnt<<endl;
if (mark[now]!=-1)
{
if (cnt-mark[now]==K)
{
findCycle=true;
return;
}
}
else
{
mark[now]=cnt;
for (int i=0; i<(int)graph[now].size(); i++)
{
if (graph[now][i]!=prev)
{
dfs(now,graph[now][i],cnt+1);
if (findCycle) break;
}
}
}
}
int main()
{
int a,b;
cin>>T;
while (T--)
{
for (int i=0; i<55; i++)
graph[i].clear();
findCycle=false;
cin>>N>>M>>K;
for (int i=0; i<M; i++)
{
cin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i=0; i<N; i++)
{
//cout<<"第"<<i<<"次dfs:"<<endl;
memset(mark,-1,sizeof(mark));
dfs(i,i,0);
if (findCycle) break;
}
if (findCycle) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
C - C
Time Limit:10000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 5543
Description
The story happened long long ago. One day, Cao Cao made a special order called “Chicken Rib” to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting idea and enjoyed it.
Xiu Yang, one of the cleverest counselors of Cao Cao, understood the command Rather than keep it to himself, he told the point to the whole army. Cao Cao got very angry at his cleverness and would like to punish Xiu Yang. But how can you punish someone because he’s clever? By looking at the chicken rib, he finally got a new idea to punish Xiu Yang.
He told Xiu Yang that as his reward of encrypting the special order, he could take as many gold sticks as possible from his desk. But he could only use one stick as the container.
Formally, we can treat the container stick as an L length segment. And the gold sticks as segments too. There were many gold sticks with different length a_{i} and value v_{i}. Xiu Yang needed to put these gold segments onto the container segment. No gold segment was allowed to be overlapped. Luckily, Xiu Yang came up with a good idea. On the two sides of the container, he could make part of the gold sticks outside the container as long as the center of the gravity of each gold stick was still within the container. This could help him get more valuable gold sticks.
As a result, Xiu Yang took too many gold sticks which made Cao Cao much more angry. Cao Cao killed Xiu Yang before he made himself home. So no one knows how many gold sticks Xiu Yang made it in the container.
Can you help solve the mystery by finding out what’s the maximum value of the gold sticks Xiu Yang could have taken?
Input
The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow. Each test case start with two integers, N(1≤N≤1000) and L(1≤L≤2000), represents the number of gold sticks and the length of the container stick. N lines follow. Each line consist of two integers, a_{i}(1≤a_{i}≤2000) and v_{i}(1≤v_{i}≤10^{9}), represents the length and the value of the i_{th} gold stick.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum value of the gold sticks Xiu Yang could have taken.
Sample Input
4
3 7
4 1
2 1
8 1
3 7
4 2
2 1
8 4
3 5
4 1
2 2
8 9
1 1
10 3
Sample Output
Case #1: 2
Case #2: 6
Case #3: 11
Case #4: 3
Hint
In the third case, assume the container is lay on x-axis from 0 to 5. Xiu Yang could put the second gold stick center at 0 and put the third gold stick center at 5,
so none of them will drop and he can get total 2+9=11 value.
In the fourth case, Xiu Yang could just put the only gold stick center on any position of [0,1], and he can get the value of 3.
C:
//贪心+DP
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
struct Stick
{
int a, v;
} s[1005];
long long dp[4005][3];
int main()
{
int t;
scanf("%d", &t);
int tt = 1;
int n, len;
while(t--)
{
scanf("%d %d", &n, &len);
len*=2;
long long ans = 0;
for(int i=1; i<=n; i++)
{
scanf("%d %d", &s[i].a, &s[i].v);
s[i].a*=2;
ans = max(ans, (long long)s[i].v);
}
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; i++)
{
for(int j=len; j>=s[i].a/2; j--)
{
for(int k=0; k<3; k++)
{
if(j>=s[i].a)
dp[j][k] = max(dp[j][k], dp[j-s[i].a][k]+s[i].v);//一般情况
if(k>0)
dp[j][k] = max(dp[j][k], dp[j-s[i].a/2][k-1]+s[i].v);//添加的物品放到边缘,那么这个物品占用量为a/2,此状态的最优解与有k-1个物品放在边缘有关。
ans = max(ans, dp[j][k]);
}
}
}
cout<<"Case #"<<tt++<<": "<<ans<<endl;
}
return 0;
}
/*
gmax(f[i][j][u],f[i-1][j-a[i].len][u]+a[i].val);
gmax(f[i][j][u],f[i-1][j-a[i].half][u-1]+a[i].val); */
D - D
Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
POJ 1984
Description
Farmer John’s pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1000) connect the farms. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n):
F1 — (13) —- F6 — (9) —– F3
| |
(3) |
| (7)
F4 --- (20) -------- F2 |
| |
(2) F5
|
F7
Being an ASCII diagram, it is not precisely to scale, of course.
Each farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path
(sequence of roads) links every pair of farms.
FJ lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road:
There is a road of length 10 running north from Farm #23 to Farm #17
There is a road of length 7 running east from Farm #1 to Farm #17
…
As FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:
What is the Manhattan distance between farms #1 and #23?
FJ answers Bob, when he can (sometimes he doesn’t yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the “Manhattan” distance between the pair of farms.
The Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points).
When Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with “-1”.
Input
* Line 1: Two space-separated integers: N and M
Lines 2..M+1: Each line contains four space-separated entities, F1,
F2, L, and D that describe a road. F1 and F2 are numbers of two farms connected by a road, L is its length, and D is a character that is either 'N', 'E', 'S', or 'W' giving the direction of the road from F1 to F2.
Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB’s
queries
Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob
and contains three space-separated integers: F1, F2, and I. F1 and F2 are numbers of the two farms in the query and I is the index (1 <= I <= M) in the data after which Bob asks the query. Data index 1 is on line 2 of the input data, and so on.
Output
Lines 1..K: One integer per line, the response to each of Bob’s
queries. Each line should contain either a distance measurement or -1, if it is impossible to determine the appropriate distance.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6 1
1 4 3
2 6 6
Sample Output
13
-1
10
Hint
At time 1, FJ knows the distance between 1 and 6 is 13.
At time 3, the distance between 1 and 4 is still unknown.
At the end, location 6 is 3 units west and 7 north of 2, so the distance is 10.
//带权并查集
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
using namespace std;
const int N=40005,K=10005;
struct node
{
int x,y,idx,n;
} a[K];
int x[N],y[N],dx[N],dy[N],rx[N],ry[N],ans[K],fa[N];
int n,m,k;
int find(int x)
{
if (fa[x]==x) return x;
int t=fa[x];
fa[x]=find(fa[x]);
rx[x]+=rx[t];
ry[x]+=ry[t];
return fa[x];
}
int cmp(node a,node b)
{
return a.idx<b.idx;
}
int main()
{
int i,j,d,fx,fy;
char c;
scanf("%d%d",&n,&m);
for (i=1; i<=n; i++)
{
fa[i]=i;
rx[i]=ry[i]=0;
}
for (i=1; i<=m; i++)
{
scanf("%d%d%d %c",&x[i],&y[i],&d,&c);
switch(c)
{
case 'W':
dx[i]=-d;
dy[i]=0;
break;
case 'S':
dx[i]=0;
dy[i]=-d;
break;
case 'E':
dx[i]=d;
dy[i]=0;
break;
case 'N':
dx[i]=0;
dy[i]=d;
break;
}
}
scanf("%d",&k);
for (i=1; i<=k; i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].idx);
a[i].n=i;
}
sort(a+1,a+k+1,cmp);
j=1;
for (i=1; i<=k; i++)
{
for (; j<=a[i].idx; j++)
{
fx=find(x[j]);
fy=find(y[j]);
fa[fy]=fx;
rx[fy]=rx[x[j]]-rx[y[j]]-dx[j];
ry[fy]=ry[x[j]]-ry[y[j]]-dy[j];
}
if (find(a[i].x)!=find(a[i].y))
ans[a[i].n]=-1;
else
ans[a[i].n]=abs(rx[a[i].x]-rx[a[i].y])+abs(ry[a[i].x]-ry[a[i].y]);
}
for (i=1; i<=k; i++)
printf("%d\n",ans[i]);
return 0;
}
E - E
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
POJ 1218
Description
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.
One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey,and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the
hall locking every other cell (cells 2, 4, 6, ?). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, ?). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He
repeats this for n rounds, takes a final drink, and passes out.
Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.
Given the number of cells, determine how many prisoners escape jail.
Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input
2
5
100
Sample Output
2
10
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
int locked[105];
int main()
{
cin.sync_with_stdio(false);
int T,n;
cin>>T;
while (T--)
{
int Ans=0;
cin>>n;
for (int i=1; i<=n; i++)
locked[i]=0;
for (int i=2; i<=n; i+=2)
locked[i]=1;
for (int j=3; j<=n; j++)
for (int i=j; i<=n; i+=j)
locked[i]^=1;
for (int i=1; i<=n; i++)
if (locked[i]==0)
Ans++;
cout<<Ans<<endl;
}
return 0;
}
F - F
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 675C
Description
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn’t like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It’s guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It’s guaranteed that the sum of all ai is equal to 0.
Output
Print the minimum number of operations required to change balance in each bank to zero.
Sample Input
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
Hint
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer:
transfer 1 from the first bank to the second bank;
transfer 3 from the second bank to the third;
transfer 6 from the third bank to the fourth.
/*我们可以将数列化为几个连续的区间,其中每个区间的数和为0,且在区间长度为K的区间中,操作数为K-1,我们就是要最大化这样的区间个数。
可以维护一个前缀和,这样两个相同的前缀之间的区间和即为0。*/
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
const int MAX=100005;
typedef long long ll;
ll A[MAX];
map<ll, int> M;
int main(void)
{
cin.sync_with_stdio(false);
int n;
cin>>n;
int Ans = 0;
ll tot = 0;
for(int i = 0; i < n; i++)
{
cin>>A[i];
tot += A[i];
M[tot]++;
Ans = max(Ans, M[tot]);
}
cout<<n-Ans<<endl;
return 0;
}