Program design thinking and practice Week6 operations (3/4 / data classes)

Krypton gold ribbon East

Original lab with a computer (No. 1), recently krypton gold band division Cuckoo East but also for the purchase of laboratory N-1 computers, numbered 2 to N. Every computer connected to a computer with a previously installed cable. But Cuckoo East worry speed is too slow, he wanted to know the i-computer network cable to a maximum length of other computers, but poor cuckoo East just recently suffered a drop of wisdom against cosmic rays, please help him.

Note: this figure corresponding to the input sample, from this figure you can see, number 1 farthest from the computer is a PC computer 4, the distance between them is 3. No. 4 and No. 5 PC computers are farthest from the point of the computer 2, so the answer is No. 2.5 No. 3 farthest from the computer PC, the computer 3 is so for its answer is 3. The same we can calculate the number 4 and No. 5 PC computer answer is 4.

Input

Input file contains several test cases. For each test, the first row of an integer N (N <= 10000), there are N-1 the next row, each row two numbers, for the i-th row of the two numbers that represent the number of computers connected to the i computer number and length of the cable between them. The total length of the cable does not exceed 10 ^ 9, separated by a space between each number.

Output

For each set of test data output line N, the answer i represents the i-th row number of the computer (1 <= i <= N ).

Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4

problem analysis

This problem is a seek diameter derived

first identify the two endpoints, the method is to select any of the recorded point to its farthest point, end point must be a diameter. Starting from one end, find the farthest point, is the other endpoint.

Seeking a maximum and then converted to a point-to-end path.

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e4+5;
struct edge{
	int p,w;
	edge(int _p, int _w):p(_p), w(_w){
	}
};
vector<edge> points[N];
int path[N];
int a, b, s;
int maxl;
void DFS(int u, int v, int l){
	int next,w;
	if(l > maxl){
		s=u;
		maxl=l;
	}
	for(int i=0; i<points[u].size(); ++i)
	{
		next=points[u][i].p;
		w=points[u][i].w;
		if(next==v)continue;
		int new_l=w + l;
		DFS(next, u, new_l);
		path[next]=max(path[next], new_l);
	} 
}
int main(){
	int n;
	while(cin>>n){
		maxl=0;
		for(int i=1;i<=n;i++){
			points[i].clear();
		}
		for(int i=2;i<=n;i++){
			cin>>a>>b;
        	points[i].push_back(edge(a,b));
        	points[a].push_back(edge(i,b));
		}
		memset(path,0,sizeof(path));
		DFS(1,0,0);
		DFS(s,0,0);
		DFS(s,0,0);
		for(int i=1;i<=n;i++){
			cout<<path[i]<<endl;
		}
	}
	return 0;
}

Wearing masks!

Novel coronavirus pneumonia (Corona Virus Disease 2019, COVID- 19), referred to as "the new crown pneumonia" refers to the 2019 novel coronavirus infection caused by pneumonia.
If an infected person into a group, then this group need to be isolated!
A small student was diagnosed with the new crown infection, and does not wear a mask! ! ! ! ! !
Risk! ! !
Time is running out! ! ! !
Need to find as quickly as possible to all students and small A direct or indirect contact through the students, they will be isolated to prevent the spread of a wider range.
As we all know, students' communication may be in small groups, a student may also participate in multiple small groups.
Please write a program to solve! wear mask! !

Input

multiple sets of data, for each test:
The first two acts of integers nand m( n = m = 0represents the input end, no treatment), nthe number of students, mthe number of groups of students. 0 < n <= 3e4 , 0 <= m <= 5e2
Student number is 0~n-1
small A numbered 0
Subsequently, mlines, each have an integer numnumber that is a small group of staff. Then there are numinteger representing the students in small groups.

Output

Number of the output to be isolated, the answer to the output of each data per line

Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1

problem analysis

Due to the large number of storage array using simulation list.

Disjoint-set, every time you look, the idea of using compression path, the current parent node directly query node becomes the root node.

The number of nodes of the final output of the answers, you need to count the number of patients and 0 with the people, we need to find the time to ensure that no group appears with a variety of value of the parent node, and 0 patients after the same parent node statistics out can be.

#include<iostream>
#include<cstdio>

using namespace std;

const int N=3e4+5;


int parent[N];

int find(int x)
{
	if(x!=parent[x]){
		int m=find(parent[x]);
		parent[x]=m;
		return m;
	}
	return x;
}

int Union(int a, int b){
	int _a=find(a);
	int _b=find(b);
	if(_a!=_b){
		parent[_a]=_b;
	}
}

int main()
{
	int m,n;
	while((~scanf("%d%d",&n,&m))&&!(m==0&&n==0))
	{
		for(int i=0;i<n;++i)
		{
			parent[i]=i;
		}
		for(int i=0;i<m;++i)
		{
			int t;
			scanf("%d",&t);
			int* temp =new int[t];
			for(int k=0;k<t;++k){
				scanf("%d",&temp[k]);
				if(k>0){
					Union(temp[k],temp[0]);
				}
			}
			delete[]temp;
		}
		int zero=find(0);
		int sum=1;
		for(int i=1;i<n;++i)
		{
			if(find(i)==zero){
				sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
 } 

Grasping magic Tokai I

Boring stuff at home in the countryside, like farming. There farmland n blocks, numbered from 1 ~ n.
To farm irrigation Rui
is well known that stuff is a magician, he can consume a certain MP in his magic on a field so that the Yellow River water to the sky. He can also consume a certain amount of MP portal established in the two drainage fields, making this piece of Tian Tian drinking water with water. (1 <= n <= 3e2 )
Yellow River water consumption is heaven Wi, i is the number of farmland (1 <= Wi <= 1e5 )
to establish a portal consumption Pij, i, j is the number of farmland (1 <= Pij <= 1e5, Pij = Pji , Pii = 0)
stuff field for all the minimum consumption of irrigation Rui

Input

Line 1: a number n
row 2 to row n + 1: a number wi
of the n + 2 th row to row 2n + 1: Matrix Matrix i.e. pij

The Output

the MP value of the minimum consumption stuff

Example

Input
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Output
9

problem analysis

Analysis of the meaning of problems and know that this is a minimum spanning tree problem.

Node 0 is assumed that water, water paths to other nodes and other nodes in the path between the known distance, simply use primthe value of minimum communication FIG.

One thing to note is that the maximum time set, preferably set to no more than 0x3ffffffffill time use fill(), or it may be wrong.

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
const int maxv=1e5+5;
const int maxn=0x7fffffff;
struct Node{
	int v,dis;
	Node(int _v, int _dis):v(_v),dis(_dis){
	}
	Node():v(0),dis(0){
	}
};
vector<Node> Adj[maxv];
int n;
int d[maxv];
bool vis[maxv]={false};
int prim(){
	fill(d,d+maxv,maxn);
	d[0]=0;
	int ans=0;
	for(int i=0;i<n;++i){
		int u=-1,MIN=maxn;
		for(int j=0;j<n;++j){
			if(vis[j]==false&&d[j]<MIN){
				u=j;
				MIN=d[j];
			}
		}
		if(u==-1) return -1;
		vis[u]=true;
		ans+=d[u];
		for(int j=0;j<Adj[u].size();++j){
			int v=Adj[u][j].v;
			if(vis[v]==false&&Adj[u][j].dis<d[v]){
				d[v]=Adj[u][j].dis;
			}
		}
	}
	return ans;
}

int main()
{
	scanf("%d",&n);
	n++;
	int t;
	for(int i=1;i<n;++i){
		scanf("%d",&t);
		Adj[0].push_back(Node(i,t));
		Adj[i].push_back(Node(0,t));
	}
	for(int i=1;i<n;++i){
		for(int j=1;j<n;++j)
		{
			scanf("%d",&t);
			if(t==0){
				continue;
			}
			Adj[i].push_back(Node(j,t));
		}
	}
	printf("%d\n",prim());
	return 0;
}

data center

See the original title http://118.190.20.162/view.page?gpid=T83

problem analysis

The title is very long, but still a problem minimum spanning tree

used the Kruskal algorithm nature or minimum spanning tree, and then output the largest side length of the selected edge.

Kruskal idea is to determine a point as the start point, each time adding a point to the shortest point, and updates its distance to other points.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n, m, i, j, u, v, total;
struct Edge{
    int start, to;
    int val;
    operator < (const Edge& edge){
    	return val<edge.val;
	}
};
Edge edge[500005];
int pre[100005];
 
int find(int x){
    if(pre[x] == x){
        return x;
    }else{
        pre[x]=find(pre[x]);
        return pre[x];
    }   
}
int kruskal(){
	int ans=0;
    for(int i = 1; i <= m; i++){
        u=find(edge[i].start);
        v=find(edge[i].to);
        if(u == v) continue;
        ans = ans>edge[i].val?ans:edge[i].val;
        pre[u] = v;
        total++;
        if(total == n - 1) break;
    }
    return ans;
}
int main(){
    cin >> n >> m;
    int root;
    cin >> root;
    for(i = 1; i <= n; i++) pre[i] = i;
    for(i = 1; i <= m; i++){
        cin >> edge[i].start >> edge[i].to >> edge[i].val;
    }
    sort(edge + 1, edge + m + 1);
    cout<<kruskal()<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/master-cn/p/12625794.html