第一题(满分)
//#include <bits/stdc++.h>
#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 10010, M = 110, INF = 0x3f3f3f3f;
int seq[N];
int countOfCite[N];
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
unordered_map<int, int> mp;
for(int j = 0; j < n; j++) {
scanf("%d", &seq[j]);
mp[seq[j]]++;
}
int maxx = -1;
for(int j = 0; j < n; j++){
int num = n - mp[seq[j]];
if(num > maxx){
maxx = num;
}
}
// cout << maxx << "----" << endl;
for(int j = 0; j < n; j++){
// cout << n - mp[seq[j]] << "---" << maxx << endl;
if(n - mp[seq[j]] == maxx) countOfCite[j]++;
}
}
int maxCite = -1, id;
for(int i = 0; i < n; i++){
if(countOfCite[i] > maxCite){
maxCite = countOfCite[i];
id = i + 1;
}
// cout << i << ":" << countOfCite[i] << endl;
}
cout << id << endl;
return 0;
}
第三题(满分)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100010;
int h[N], ne[N], e[N], idx;
int cnt[N];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dfs(int u){
cnt[u]++;
for(int i = h[u]; i != -1; i = ne[i]){
int j = e[i];
cnt[u] += dfs(j);
}
return cnt[u];
}
int main(){
memset(h, -1, sizeof h);
int n; cin >> n;
for(int i = 2; i <= n; i++){
int fa;
scanf("%d",&fa);
add(fa, i);
}
int sum = dfs(1);
int q; cin >> q;
for(int i = 0; i < q; i++){
int t; scanf("%d", &t);
cout << cnt[t] << endl;
}
return 0;
}
第四题(-8分)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 2010, INF = 0x3f3f3f3f;
int n, m;
int g[N][N];
int ddl[N], Y[N], P[N], start_time;
void floyd(){
for(int k = 0; k <= n; k++)
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
if(g[i][k] + g[k][j] < g[i][j])
g[i][j] = g[i][k] + g[k][j];
}
bool cmp(PII a, PII b){
if(a.first == b.first){
return a.second < b.second;
}else{
return a.first > b.first;
}
}
string get_time(int t){
int hh = t / 60, mm = t % 60;
string h, m;
if(hh == 0) h = "00";
else h = to_string(hh);
if(mm == 0) m = "00";
else m = to_string(mm);
if(h.size() == 1) h = "0" + h;
if(m.size() == 1) m = "0" + m;
return h + ":" + m;
}
int main(){
memset(g, 0x3f, sizeof g);
cin >> n >> m;
int hh, mm;
scanf("%d:%d", &hh, &mm);
start_time = hh * 60 + mm;
for(int i = 1; i <= n; i++){
scanf("%d:%d", &hh, &mm);
ddl[i] = hh * 60 + mm;
scanf("%d%d", &Y[i], &P[i]);
}
ddl[0] = 23 * 60 + 59, Y[0] = 0, P[0] = 0;
for(int i = 0; i < m; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = g[b][a] = min(g[a][b], c);
}
floyd();
int q, stop[N]; cin >> q;
bool st[N];
int res_t = INF, res_price = -1;
while(q--){
stop[0] = stop[n+1] = 0;
memset(st, 0, sizeof st);
for(int i = 1; i <= n; i++) cin >> stop[i], st[stop[i]] = true;
bool each = true;
for(int i = 1; i <= n; i++){
if(st[i] == false){
each = false;
break;
}
}
if(each == false) continue;
int t = start_time, price = 0;
bool flag = true;
bool st[N];
for(int i = 0; i < n+1; i++){
int a = stop[i], b = stop[i+1];
if(g[a][b] > INF / 2) continue;
t += g[a][b];
if(t > ddl[0]){
flag = false;
break;
}
if(t <= ddl[b]) price += Y[b];
else if(t > ddl[b]) price += Y[b] - P[b];
}
if(flag){
if(price > res_price){
res_price = price;
res_t = t;
}else if(price == res_price && t < res_t){
res_t = t;
}
}
}
cout << res_price << " " << get_time(res_t) << endl;
return 0;
}