You are given a rectangular cake, represented as an r × c grid. Each cell either has an evil strawberry, or is empty. For example, a3 × 4 cake may look as follows:
The cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least one cake cell that has not been eaten before, and eats all the cake cells there. He may decide to eat any number of times.
Please output the maximum number of cake cells that the cakeminator can eat.
The first line contains two integers r andc (2 ≤ r, c ≤ 10), denoting the number of rows and the number of columns of the cake. The nextr lines each contains c characters — the j-th character of thei-th line denotes the content of the cell at rowi and columnj, and is either one of these:
- '.' character denotes a cake cell with no evil strawberry;
- 'S' character denotes a cake cell with an evil strawberry.
Output the maximum number of cake cells that the cakeminator can eat.
3 4 S... .... ..S.
8
For the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats).
链接:http://codeforces.com/contest/330/problem/A
代码:
#include<cstdio> #include<iostream> #include<cstring> using namespace std; char maps[12][12]; int vis[12][12]; int flag1[12],flag2[12]; //用来标记行和列 int r,c,s; int main() { int i,j; while(scanf("%d%d",&r,&c)!=EOF) { getchar(); memset(vis,0,sizeof(vis)); memset(flag1,0,sizeof(flag1)); memset(flag2,0,sizeof(flag2)); for(i=0;i<r;i++) { gets(maps[i]); for(j=0;j<c;j++) { if(maps[i][j]=='S') { flag1[i]=1; flag2[j]=1; } } } s=0; for(i=0;i<r;i++) { if(flag1[i]==1) continue; for(j=0;j<c;j++) { if(vis[i][j]==0) { vis[i][j]=1; s++; } } } for(j=0;j<c;j++) { if(flag2[j]==1) continue; for(i=0;i<r;i++) { if(vis[i][j]==0) { vis[i][j]=1; s++; } } } printf("%d ",s); } return 0; }
A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also givenm pairs of cities — roads cannot be constructed between these pairs of cities.
Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.
The first line consists of two integers n andm.
Then m lines follow, each consisting of two integersai andbi (1 ≤ ai, bi ≤ n,ai ≠ bi), which means that it is not possible to construct a road connecting cities ai andbi. Consider the cities are numbered from 1 ton.
It is guaranteed that every pair of cities will appear at most once in the input.
You should print an integer s: the minimum number of roads that should be constructed, in the first line. Thens lines should follow, each consisting of two integersai andbi (1 ≤ ai, bi ≤ n, ai ≠ bi), which means that a road should be constructed between cities ai andbi.
If there are several solutions, you may print any of them.
4 1 1 3
3 1 2 4 2 2 3
This is one possible solution of the example:
These are examples of wrong solutions:
链接:http://codeforces.com/contest/330/problem/B
思路:利用对称,找中心节点。
代码:
#include<cstdio> #include<iostream> #include<cstring> using namespace std; bool flag[1010]; int main() { int n,m,a,b; while(scanf("%d%d",&n,&m)!=EOF) { memset(flag,0,sizeof(flag)); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); flag[a]=true; flag[b]=true; } int u; for(int j=1;j<=n;j++) { if(flag[j]==false) u=j; } printf("%d ",n-1); for(int k=1;k<=n;k++) { if(k!=u) printf("%d %d ",k,u); } } return 0; }
You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming ann × n grid. The rows are numbered1 through n from top to bottom, and the columns are numbered1 throughn from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door:
Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells.
The only method of tile purification known to you is by casting the "Purification" spell. You cast this spell on a single tile — then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once.
You would like to purify all n × n cells while minimizing the number of times you cast the "Purification" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the "Purification" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the "Purification" spell.
Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way.
The first line will contain a single integer n (1 ≤ n ≤ 100). Then,n lines follows, each containsn characters. The j-th character in thei-th row represents the cell located at rowi and columnj. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise.
If there exists no way to purify all the cells, output -1. Otherwise, if your solution castsx "Purification" spells (wherex is the minimum possible number of spells), outputx lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the "Purification" spell.
3 .E. E.E .E.
1 1 2 2 3 3
3 EEE E.. E.E
-1
5 EE.EE E.EE. E...E .EE.E EE.EE
3 3 1 3 2 2 4 4 5 3
The first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which "Purification" is cast. Yellow tiles are the tiles being purified as a result of the current "Purification" spell. Green tiles are tiles that have been purified previously.
In the second example, it is impossible to purify the cell located at row 1 and column 1.
For the third example:
跟330A那题比较类似,如果你要清除一个n*n的正方形,你知道只用放n个点就能清除所有的方块。
如
*****
.....
.....
.....
.....
*的就是清除他们的点。同理竖着的,横着的,斜着的。所以我们只用构造出这样的5个点就可以了。所以题目转换成判断是否存在这样的n个点,以及如何放的问题。
第一类:
EEEEE
E....
E....
E....
E....
这是无解的情况,因为(1,1)这个点无法清除。所以判断无解的情况只要检索是否存在一行都是E并且一列都是E这种就OK了。
第2类:
EEEEE
E....
E.E..
E..E.
.....
像这种,横着存在全是E情况的,只用在每竖行找到一个能放置的点就可以了(一定存在的,不然就是上面所说的无解情况了)。
第3类:
EEEE.
E....
E.E..
E.E..
E....
这种类似于第2类分析。每个横行找到一个能放置的点就可以了(一定存在的,不然就是上面所说的无解情况了)。
如果是第4类这样的
EEEE.
E....
E....
E....
.....
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char maps[105][105]; int row[105],col[105]; int n; int r,c; int main() { int i,j; while(scanf("%d",&n)!=EOF) { getchar(); r=0,c=0; memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); for(i=1;i<=n;i++) gets(maps[i]+1); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(maps[i][j]=='E') { row[i]++; col[j]++; if(row[i]==n) r=1; if(col[j]==n) c=1; if(r&&c) { puts("-1"); return 0; } } } } if(r) { for(j=1;j<=n;j++) { for(i=1;i<=n;i++) { if(maps[i][j]=='.') { printf("%d %d ",i,j); break; } } } } else { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(maps[i][j]=='.') { printf("%d %d ",i,j); break; } } } } } return 0; }
You're a mikemon breeder currently in the middle of your journey to become a mikemon master. Your current obstacle is go through the infamous Biridian Forest.
The forest
The Biridian Forest is a two-dimensional grid consisting of r rows and c columns. Each cell in Biridian Forest may contain a tree, or may be vacant. A vacant cell may be occupied by zero or more mikemon breeders (there may also be breeders other than you in the forest). Mikemon breeders (including you) cannot enter cells with trees. One of the cells is designated as the exit cell.
The initial grid, including your initial position, the exit cell, and the initial positions of all other breeders, will be given to you. Here's an example of such grid (from the first example):
Moves
Breeders (including you) may move in the forest. In a single move, breeders may perform one of the following actions:
- Do nothing.
- Move from the current cell to one of the four adjacent cells (two cells are adjacent if they share a side). Note that breeders cannot enter cells with trees.
- If you are located on the exit cell, you may leave the forest. Only you can perform this move — all other mikemon breeders will never leave the forest by using this type of movement.
After each time you make a single move, each of the other breeders simultaneously make a single move (the choice of which move to make may be different for each of the breeders).
Mikemon battle
If you and t (t > 0) mikemon breeders are located on the same cell, exactlyt mikemon battles will ensue that time (since you will be battling each of thoset breeders once). After the battle, all of thoset breeders will leave the forest to heal their respective mikemons.
Note that the moment you leave the forest, no more mikemon battles can ensue, even if another mikemon breeder move to the exit cell immediately after that. Also note that a battle only happens between you and another breeders — there will be no battle between two other breeders (there may be multiple breeders coexisting in a single cell).
Your goal
You would like to leave the forest. In order to do so, you have to make a sequence of moves, ending with a move of the final type. Before you make any move, however, you post this sequence on your personal virtual idol Blog. Then, you will follow this sequence of moves faithfully.
Goal of other breeders
Because you post the sequence in your Blog, the other breeders will all know your exact sequence of moves even before you make your first move. All of them will move in such way that will guarantee a mikemon battle with you, if possible. The breeders that couldn't battle you will do nothing.
Your task
Print the minimum number of mikemon battles that you must participate in, assuming that you pick the sequence of moves that minimize this number. Note that you are not required to minimize the number of moves you make.
The first line consists of two integers: r andc (1 ≤ r, c ≤ 1000), denoting the number of rows and the number of columns in Biridian Forest. The nextr rows will each depict a row of the map, where each character represents the content of a single cell:
- 'T': A cell occupied by a tree.
- 'S': An empty cell, and your starting position. There will be exactly one occurence of this in the map.
- 'E': An empty cell, and where the exit is located. There will be exactly one occurence of this in the map.
- A digit (0-9): A cell represented by a digit X means that the cell is empty and is occupied by X breeders (in particular, if X is zero, it means that the cell is not occupied by any breeder).
It is guaranteed that it will be possible for you to go from your starting position to the exit cell through a sequence of moves.
A single line denoted the minimum possible number of mikemon battles that you have to participate in if you pick a strategy that minimize this number.
5 7 000E0T3 T0TT0T0 010T0T0 2T0T0T0 0T0S000
3
1 4 SE23
2
The following picture illustrates the first example. The blue line denotes a possible sequence of moves that you should post in your blog:
The three breeders on the left side of the map will be able to battle you — the lone breeder can simply stay in his place until you come while the other two breeders can move to where the lone breeder is and stay there until you come. The three breeders on the right does not have a way to battle you, so they will stay in their place.
For the second example, you should post this sequence in your Blog:
Here's what happens. First, you move one cell to the right.
Then, the two breeders directly to the right of the exit will simultaneously move to the left. The other three breeder cannot battle you so they will do nothing.
You end up in the same cell with 2 breeders, so 2 mikemon battles are conducted. After those battles, all of your opponents leave the forest.
Finally, you make another move by leaving the forest.
思路:从终点开始BFS,搜到起点的下一步。步数小于等于终点到起点的步数的人数相加。因为可以都看成在终点等着和起点出发的人打架。
代码:先不贴了。老是WA在第8组上。还在调。
I have an undirected graph consisting of n nodes, numbered 1 throughn. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.
I would like to create a new graph in such a way that:
- The new graph consists of the same number of nodes and edges as the old graph.
- The properties in the first paragraph still hold.
- For each two nodes u and v, if there is an edge connecting them in the old graph, there is no edge connecting them in the new graph.
Help me construct the new graph, or tell me if it is impossible.
The first line consists of two space-separated integers: n and m (1 ≤ m ≤ n ≤ 105), denoting the number of nodes and edges, respectively. Thenm lines follow. Each of the m lines consists of two space-separated integers u and v (1 ≤ u, v ≤ n;u ≠ v), denoting an edge between nodes u and v.
If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactlym lines. Each line should contain a description of edge in the same way as used in the input format.
8 7 1 2 2 3 4 5 5 6 6 8 8 7 7 4
1 4 4 6 1 6 2 7 7 5 8 5 2 8
3 2 1 2 2 3
-1
5 4 1 2 2 3 3 4 4 1
1 3 3 5 5 2 2 4
The old graph of the first example:
A possible new graph for the first example:
In the second example, we cannot create any new graph.
The old graph of the third example:
A possible new graph for the third example:
链接:http://codeforces.com/contest/330/problem/E
分析:随机算法。
代码:
//保证度数为2,那么不是线就是环 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; vector<int> djw; vector<int> edge[110000]; int n,m; void solve() { bool tt=false; for(int i=1;i<=500;i++) //随机次数 { random_shuffle(djw.begin(),djw.end()); //随机节点 bool flag=true; int p,q; for(int j=0;j<m&&flag;j++) //判断是否满足 { p=djw[j]; q=djw[(j+1)%n]; for(int k=0;k<edge[p].size();k++) if(edge[p][k]==q) flag=false; } if(flag) //如果满足直接输出 { for(int j=0;j<m;j++) printf("%d %d ",djw[j],djw[(j+1)%n]); tt=true; break; } } if(!tt) printf("-1 "); } int main() { int a,b; while(~scanf("%d%d",&n,&m)) { djw.clear(); for(int i=1;i<=n;i++) { edge[i].clear(); djw.push_back(i); } //printf("%d ",djw[1]); for(int j=1;j<=m;j++) { scanf("%d%d",&a,&b); edge[a].push_back(b); edge[b].push_back(a); } solve(); } return 0; }