P5318 【深基18.例3】查找文献 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:
利用两个vector数组,用一个结构体vector(为了节省空间,咱用vector来存)存储每个边的起点和终点,然后用一个二维vector(也就是一个vector数组)存储边的信息。依次对其进行dfs和bfs遍历(队列)
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<vector>
using namespace std;
struct edge
{int u;int v;
};
vector <int>e[100005];
vector <edge>s;
int vis1[100005];
int vis2[100005];
bool cmp(edge a,edge b)
{if (a.u = b.u){return a.v < b.v;}else{return a.u < b.u;}
}
void dfs(int x)
{vis1[x] = 1;cout << x << " ";for (int i = 0; i < e[x].size(); i++){int pos = s[e[x][i]].v;if (!vis1[pos]){dfs(pos);}}
}
void bfs(int x)
{queue<int>q;q.push(x);cout << x << " ";vis2[x] = 1;while (!q.empty()){int front = q.front();for (int i = 0; i < e[front].size(); i++){int pos = s[e[front][i]].v;if (!vis2[pos]){q.push(pos);cout << pos << " ";vis2[pos] = 1;}}q.pop();}
}
int main()
{int n, m;cin >> n >> m;for (int i = 0; i < m; i++){int uu, vv;cin >> uu >> vv;s.push_back(edge{uu,vv});}sort(s.begin(), s.end(), cmp);for (int i = 0; i < m; i++){e[s[i].u].push_back(i);}dfs(1);cout << endl;bfs(1);return 0;
}
Dashboard - The 20th Southeast University Programming Contest (Summer) - Codeforces
Problem - L - Codeforces
思路:
理解一下题意就能明白,题目的意思是取一个值(即在原有序列中直接去掉这个值),要得到<=这个值的最大值,其实就是得到序列中除最大值之外的次大值。
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a, int b)
{return a < b;
}
int main()
{int t, ans;cin >> t;while (t--){int n;cin >> n;int a[105];for (int i = 0; i < n; i++){cin >> a[i];}sort(a, a + n, cmp);/*for (int i = 0; i < n; i++){cout << a[i];}*/ans = a[0];for (int i = 0; i < n; i++){if (a[i] != a[n - 1]&&a[i]<a[n-1]){ans = a[i];}}cout << ans << endl;}return 0;
}