当前位置:网站首页>Cow sequencing problem
Cow sequencing problem
2022-06-26 02:23:00 【chengqiuming】
One Original problem description
3275 -- Ranking the Cows
http://poj.org/problem?id=3275
Two Input and output
1 Input
The first 1 Line contains two integers N and M. The first 2 To M+1 That's ok , Each line contains two integers X and Y.X and Y It's all in 1 To N Within the scope of , A cow X Ranking higher than cows Y.
2 Output
At least how many relationships must be investigated for single row output to complete the whole sorting .
3 sample input
5 5
2 1
1 5
2 3
1 4
3 4
4 sample output
3
3、 ... and Algorithm analysis and Design
1 According to the input sample , Create a directed graph

2 According to Transitivity , Got 7 Kind of relationship , Namely
1>4
1>5
2>1
2>3
2>4
2>5
3>4
3 For having n Graph of nodes , The relationship between two people has n(n-1)/2 Kind of ,5 Nodes share 10 Kind of relationship , Also need to know 10-7 = 3 Kind of relationship .
You can use bitset operation , Use one for each node bitset To describe .
bitset<maxn> p[maxn]; // maxn Number of digits ,p[] Represents a binary array
Initialization time ,p[i][i]=1, namely p[i] Of the i Position as 1( Number from the right 0 position , The first 1 position , The first 2 position ).
Input 1-5, Give Way p[1][5] = 1( The first 1 The milk yield of a cow is greater than that of the 5 head ), be p[1]=...100010
Input 1-4, Give Way p[1][4] = 1( The first 1 The milk yield of a cow is greater than that of the 4 head ), be p[1]=...110010
Input 2-1, Give Way p[2][1] = 1( The first 2 The milk yield of a cow is greater than that of the 1 head ), be p[2]=...000110
Input 2-3, Give Way p[2][3] = 1( The first 2 The milk yield of a cow is greater than that of the 3 head ), be p[2]=...001110
Input 3-4, Give Way p[3][4] = 1( The first 3 The milk yield of a cow is greater than that of the 4 head ), be p[3]=...011000
Determine each bit of each array , The following logic exists
if(p[i][j]){
p[i] = p[i]|p[j]; // Bitwise OR operation ,i Cows produce more milk than j The milk yield of a cow , therefore j The milk production relationship between cows and other cows can be passed on to i, So use or relation
}for example :p[2][1]=1, be p[2]=p[2]|p[1]=001110|110010=111110, cow 2 And cows 1 It matters , Then cow 1 To the cows 2.
In this way , You can find the relationship between each point and other points . use ans Accumulate each array element 1 The number of , Because when initializing, you can set yourself to 1, So forget it n Kind of relationship , Need to subtract .
Four Code
package graph;
import java.util.BitSet;
import java.util.Scanner;
public class POJ3275 {
public static void main(String[] args) {
int n, m;
Scanner scanner = new Scanner(System.in);
// n Head ox
n = scanner.nextInt();
// m A relationship
m = scanner.nextInt();
BitSet p[] = new BitSet[n + 1];
for (int i = 1; i <= n; i++) {
p[i] = new BitSet(n + 1);
}
for (int i = 1; i <= n; i++)
p[i].set(i);
while (m-- != 0) {
int u, v;
u = scanner.nextInt();
v = scanner.nextInt();
p[u].set(v);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (p[i].get(k))
p[i].or(p[k]);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (p[i].get(j)) {
ans++;
}
}
}
System.out.println(n * (n - 1) / 2 - ans + n);
}
}5、 ... and test
Green is the input , White is the output

边栏推荐
- heaven and hell's moving
- Cross server SQL connection configuration
- How to set an achievable annual goal?
- Raspberry pie + AWS IOT introductory experiment
- V4l2+qt video optimization strategy
- 数字商品DGE--数字经济的财富黑马
- Bloc入门之Cubit详解
- 哪个证券公司手机股票开户更好更安全?
- How to set achievable medium - and long-term goals?
- 树莓派 + AWS IoT 入门实验
猜你喜欢
随机推荐
PyQt theme
短信插件哪个好用万能表单需要发短信着急测试
How to efficiently complete daily tasks?
Timer case
奶牛排序问题
SDRAM控制器——添加读写FIFO
What happens when the cloud answer does not display the third-party login button
基于邻接矩阵的广度优先遍历
樹莓派 + AWS IoT Greengrass
Record a weird picture upload problem
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
Breadth first traversal based on adjacency table
Tab switch
Graduation summary of cloud native training camp
How to use commands to write file names (including paths) in folders to txt files
Wechat applet
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
其他代码,,vt,,,k
Shell learning record (II)
A solution to cross domain problems








