每日三百行代码 第二十六天
#include<bits/stdc++.h>
using namespace std;
const int maxn = 510;
vector<int> edge[maxn];
bool book[maxn] = {
false};
void dfs(int u) {
book[u] = true;
for (int i = 0; i < edge[u].size(); i++) {
if (!book[edge[u][i]]) dfs(edge[u][i]);
}
}
int main() {
int n, m, u, v;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
u++, v++;
edge[u].push_back(v);
edge[v].push_back(u);
}
int k, city, cnt = 0;
for (int i = 1; i <= n; i++) {
if (book[i] == false && !edge[i].empty()) {
dfs(i);
cnt++;
}
}
scanf("%d", &k);
for (int j = 0; j < k; j++) {
scanf("%d", &city);
city++;
int tmp = 0;
bool flag = false;
memset(book, false, sizeof(book));
edge[city].clear();
for (int i = 1; i <= n; i++) {
if (book[i] == false && !edge[i].empty()) {
dfs(i);
tmp++;
}
}
if (tmp > cnt) printf("Red Alert: City %d is lost!\n", city - 1);
else printf("City %d is lost.\n", city - 1);
cnt = tmp;
}
if (k == n) printf("Game Over.\n");
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 505;
int G[N][N];
bool vis[N];
int n, m;
void dfs(int u){
vis[u] = true;
for(int i = 0 ; i < n; i++){
if(G[u][i] == 1 && !vis[i]) dfs(i);
}
}
int main(){
cin>>n>>m;
int u, v;
while(m--){
cin>>u>>v;
G[u][v] = 1;
G[v][u] = 1;
}
int k, x;
cin>>k;
int pre = 0;
memset(vis, false, sizeof vis);
for(int i = 0; i < n; i++){
if(!vis[i]){
dfs(i);
pre++;
}
}
vector<int> st;
for(int i = 0; i < k; i++){
memset(vis, false, sizeof vis);
int cnt = 0;
cin>>x;
st.push_back(x);
for(auto it: st) vis[it] = true;
for(int i = 0; i < n; i++){
if(!vis[i]){
dfs(i);
cnt++;
}
}
if(cnt > pre) printf("Red Alert: City %d is lost!\n", x);
else printf("City %d is lost.\n", x);
pre = cnt;
if(i == n - 1) printf("Game Over.");
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
cin>>n;
set<int> s;
for(int i = 0; i<n; i++)
{
cin>>t;
set<int>::iterator it = s.lower_bound(t);
if(it!=s.end())
{
s.insert(t);
s.erase(*it);
}
else s.insert(t);
}
cout<<s.size();
}
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
set<int> s;
int n;
cin>>n;
for(int i=0;i<n;i++){
int a;
cin>>a;
if(s.upper_bound(a) != s.end()){
s.erase(s.upper_bound(a));
}
s.insert(a);
}
cout<<s.size()<<endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
bool cmp(float x, float y) {
if (x > y)
return true;
return false;
}
int main() {
int N, K, M;
cin >> N >> K >> M;
float* students = new float[N];
for (int i = 0; i < N; i++) {
float max = -1,min=101;
float sum = 0;
for (int j = 0; j < K; j++) {
int grade;
cin >> grade;
sum += grade;
if (max < grade)
max = grade;
if (min > grade)
min = grade;
}
sum = sum - max - min;
sum = sum / (K - 2);
students[i] = sum;
}
sort(students, students + N,cmp);
printf("%.3f", students[M - 1]);
for (int i = 1; i < M; i++) {
printf(" %.3f", students[M-i-1]);
}
cout << endl;
delete[]students;
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
typedef struct Node{
double score;
}Node;
void shellinsert(Node *a,int n,int dk){
Node temp;
for(int i=dk;i<n;i+=dk){
if(a[i].score<a[i-dk].score){
temp=a[i];
int j;
for(j=i-dk;j>=0&&a[j].score>temp.score;j-=dk){
a[j+dk]=a[j];
}
a[j+dk]=temp;
}
}
}
void shellsort(Node *a,int n){
int dk[12]={
5000,2500,1000,500,250,125,75,30,15,8,4,1};
for(int i=0;i<12;i++){
shellinsert(a,n,dk[i]);
}
}
int main(){
int n,k,m;
scanf("%d %d %d",&n,&k,&m);
Node a[n];
for(int i=0;i<n;i++){
int e;
int max=-999;
int min=999;
int sum=0;
for(int j=0;j<k;j++){
scanf("%d",&e);
sum+=e;
if(e>max)max=e;
if(e<min)min=e;
}
a[i].score=(sum-max-min)*1.0/(k-2);
}
shellsort(a,n);
if(m>n)m=n;
int flag=0;
for(int i=n-m;i<n;i++){
if(flag==1){
printf(" %.3f",a[i].score);
}else{
flag=1;
printf("%.3f",a[i].score);
}
}
printf("\n");
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n;
struct people{
int sex,father=-1,mother=-1;
}peo[N];
bool st[N],flag;
void dfs(int cur,int generation)
{
if(cur==-1) return;
if(generation>5) return;
st[cur]=1;
dfs(peo[cur].father,generation+1);
dfs(peo[cur].mother,generation+1);
}
void finddfs(int cur,int generation)
{
if(cur==-1) return;
if(generation>5) return;
if(st[cur]) flag=true;
finddfs(peo[cur].father,generation+1);
finddfs(peo[cur].mother,generation+1);
}
int main()
{
cin>>n;
int id,father,mother;
string sex;
for(int i=0;i<n;i++)
{
cin>>id>>sex;
if(sex=="M") peo[id].sex=1;
else peo[id].sex=0;
cin>>peo[id].father>>peo[id].mother;
peo[peo[id].father].sex=1;
peo[peo[id].mother].sex=0;
}
cin>>n;
int a,b;
for(int i=0;i<n;i++)
{
cin>>a>>b;
if(peo[a].sex==peo[b].sex) puts("Never Mind");
else{
memset(st,false,sizeof st);
dfs(a,1);
flag=false;
finddfs(b,1);
if(flag) puts("No");
else puts("Yes");
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
int result;
struct Person {
int sex;
vector<int> parent;
};
map<int, Person> people;
void judge(int start, int end, int len) {
if (start == end)
result = 1;
if (len<4)
for (auto &i: people[start].parent)
for (auto &j: people[end].parent)
judge(i, j, len + 1);
}
int main() {
int n, m, id, father, mother;
char sex;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> id >> sex >> father >> mother;
people[id].sex=sex=='M' ? 1 : 0 ;
people[father].sex = 1;
if (father != -1)
people[id].parent.push_back(father);
if (mother != -1)
people[id].parent.push_back(mother);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> father >> mother;
if (people[father].sex == people[mother].sex)
cout << "Never Mind" << endl;
else {
result = 0;
judge(father, mother, 0);
cout << (result ? "No" : "Yes") << endl;
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using namespace std;
int main(){
int N,Sum,sum=0;
cin>>N;
vector<int>res;
for(int i=1;i<=N;i++){
int num;
cin>>num;
res.push_back(num);
}
sort(res.begin(),res.end());
for(int i=0;i<N/2;i++)
sum+=res[i];
Sum=accumulate(res.begin(),res.end(),0);
cout<<"Outgoing #: "<<N-N/2<<endl;
cout<<"Introverted #: "<<N/2<<endl;
cout<<"Diff = "<<abs((Sum-sum)-sum);
return 0;
}
#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;
int main()
{
int i,j,n,m,k,t,l,Min=0,Max=0;
priority_queue<int,vector<int>,greater<int> > q;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&l);
q.push(l);
}
for(i=0;i<n;i++)
{
int tmp=q.top();
q.pop();
if(i<n/2)
{
Min+=tmp;
}
else
{
Max+=tmp;
}
}
if(n%2==0)
{
printf("Outgoing #: %d\n",n/2);
printf("Introverted #: %d\n",n/2);
printf("Diff = %d",Max-Min);
}
else
{
printf("Outgoing #: %d\n",n/2+1);
printf("Introverted #: %d\n",n/2);
printf("Diff = %d",Max-Min);
}
return 0;
}