ac代码
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
using namespace std;
struct Peo{
int age;
char name[20];
bool operator<(const Peo &b) {
if (age == b.age)return (strcmp(name, b.name) < 0);
return age > b.age;
}
}a[105];
int main(){
char fa[105][20], ch[105][20];
int c, t, n, year[105];
cin >> c;
a[0].age = 100;
strcpy(a[0].name, "Ted");
for (t = 1; t <= c; t++){
cin >> n;
for (int i = 1; i <= n; i++)
cin >> fa[i] >> ch[i] >> year[i];
for (int i = 0, k = 1; i < n; i++)
for (int j = 1; j <= n; j++)
if (strcmp(a[i].name, fa[j]) == 0) {
a[k].age = a[i].age - year[j];
strcpy(a[k].name, ch[j]);
k++;
}
sort(a, a + n + 1);
cout << "DATASET " << t << endl;
for (int i = 1; i <= n; i++)
cout << a[i].name << ' ' << a[i].age << endl;
}
return 0;
}
我的代码
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
using namespace std;
struct Peo {
string name;
int fat,id,big;
int age;
bool is_new;
Peo():age(-1), is_new(false){}
bool operator<(const Peo& b) {
if (age == b.age)
return name < b.name;
return age > b.age;
}
}a[111];
void solve(int t) {
int n,age,len = 0;
cin >> n;
map<string, int> mp;
string s1, s2;
for (int i = 0; i < n; i++) {
cin >> s1 >> s2 >> age;
if (!mp.count(s1))
mp[s1] = len++;
if (!mp.count(s2))
mp[s2] = len++;
int fid = mp[s1], sid = mp[s2];
if (a[fid].is_new == false) {
a[fid].name = s1;
//cout << a[fid].name << endl;
a[fid].id = fid;
a[fid].is_new = true;
}
if (a[sid].is_new == false) {
a[sid].name = s2;
//cout << a[sid].name << endl;
a[sid].id = sid;
a[sid].is_new = true;
}
a[sid].big = age;
a[sid].fat = fid;
}
int ted = mp["Ted"];
a[ted].age = 100;
queue<int> q;
q.push(ted);
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = 0; i < len; i++) {
if (a[i].fat == now && i!= ted) {
a[i].age = a[now].age - a[i].big;
q.push(i);
}
}
}
sort(a, a + n + 1);
cout << "DATASET " << t << endl;
for (int i = 1; i <= n; i++)
cout << a[i].name << ' ' << a[i].age << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
for (int i = 1; i <= t; i++)
solve(i);
return 0;
}