PAT Advanced 1139 First Contact (30)
题目描述
Input Specification:
Output Specification:
Sample Input:
Sample Output:
解题思路
2019.2.18更新:之前的代码在换测试点后在最后一个测试带你超时。。
u想追求v, 找出u的同性好朋友u1,v的同性好朋友v1,并且,u1和v1也得是好朋友。其中输入中带负号的为female。
注意点:不能用int定义输入数据,因为输入中含 -0 这种数据代表女生,用string存(被大神提醒)。每个人的id唯一,也就是不存在一个男生的id等于一个女生id的绝对值(暂时这么说),因此可以用她们的id进行存储。其他坑点,比如u的好友中有v,即u1=v, 然后此时是会绕回去的,这种情况应该continue。再比如v1=u,而u的好友中又有v,这种情况也应该跳过。另外输出id应该以4为数字的形式,不足补0。其他分析见代码。
Code
- AC代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
struct Node{
int u, v;
};
int N, M;
int sex[maxn];
unordered_map<int, int> m;
vector<int> friends[maxn];
bool cmp(const Node &a, const Node &b) {
return a.u != b.u ? a.u < b.u : a.v < b.v;
}
int str2int(const string &str) {
int id = 0, neg = 0, i = 0;
if(str[i] == '-') {
neg = 1;
i++;
}
for(; i<str.length(); i++) {
id = id*10 + str[i]-'0';
}
if(neg) sex[id] = -1;
else sex[id] = 1;
return id;
}
void solve(const int &a, const int &b) {
vector<Node> res;
for(int i = 0; i<friends[a].size(); i++) {
int c = friends[a][i];
if(c == b || sex[a]*sex[c] != 1) continue;
for(int j = 0; j<friends[b].size(); j++) {
int d = friends[b][j];
if(d == a || sex[b]*sex[d] != 1) continue;
if(m[c*10000+d] == 1) {
res.push_back({c, d});
}
}
}
printf("%d\n", res.size());
if(res.size()) {
sort(res.begin(), res.end(), cmp);
for(int i = 0; i<res.size(); i++) {
printf("%04d %04d\n", res[i].u, res[i].v);
}
}
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin >> N >> M;
string u, v;
int a, b;
for(int i = 0; i<M; i++) {
cin >> u >> v;
a = str2int(u), b = str2int(v);
if(u.length() == v.length()) {
friends[a].push_back(b);
friends[b].push_back(a);
}
m[a*10000+b] = m[b*10000+a] = 1;
}
int K;
cin >> K;
for(int i = 0; i<K; i++) {
cin >> a >> b;
solve(abs(a), abs(b));
}
return 0;
}
- 之前代码,最后一个测试点超时
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+100;
struct Node{
bool isF;
vector<int> fri;
}node[maxn];
int G[maxn][maxn];
int n,m,u,v,k,u1,v1;
int toNum(string s){
int j = 0, res = 0;
if(s[0] == '-') j++;
for( ; j<s.length(); j++){
res = res*10 + s[j]-'0';
}
if(s[0] == '-') node[res].isF = true; //有负号则说明是female
return res;
}
string to_str(int x){
ostringstream ss;
ss<<x;
string res = ss.str();
while(res.length() != 4){
res = "0"+res; //不足4位则往前补0
}
return res;
}
void solve(){
//u和u1是同性好基友 v和v1是同性好基友 u喜欢v
set<string> ans; //用set存放结果则省去了排序的步骤
for(int i = 0; i<node[u].fri.size(); i++){
u1 = node[u].fri[i];
//当u的朋友中有v时应该跳过
if(u1 == v) continue;
//判断u和u1是不是同性
if(node[u].isF && !node[u1].isF) continue;
if(node[u1].isF && !node[u].isF) continue;
for(int j = 0; j<node[u1].fri.size(); j++){
v1 = node[u1].fri[j];
//当v1是u时跳过
if(v1 == u) continue;
//判断v和v1是不是同性
if(node[v1].isF && !node[v].isF) continue;
if(node[v].isF && !node[v1].isF) continue;
if(G[v1][v] == 0) continue;
string tem = to_str(u1)+" "+to_str(v1);
ans.insert(tem);
}
}
cout<<ans.size()<<endl;
for(set<string>::iterator it = ans.begin(); it!=ans.end(); it++){
cout<<*it<<endl;
}
}
int main(){
//freopen("in.txt", "r", stdin);
cin>>n>>m;
string str1,str2;
for(int i = 0; i<m; i++){
cin>>str1>>str2;
u = toNum(str1);
v = toNum(str2);
node[u].fri.push_back(v);
node[v].fri.push_back(u);
G[u][v]=G[v][u]=1;
}
cin>>k;
for(int i = 0; i<k; i++){
cin>>str1>>str2;
u = toNum(str1);
v = toNum(str2);
solve();
}
return 0;
}
总结
const string &str
真香
在定义函数参数的时候,把参数定义成常量传引用的形式可以节省很大一部分时间,因为普通的参数在调用函数时是复制一份实参的拷贝到函数里执行,拷贝需要一定的时间。或者直接传引用&
进去能节省拷贝的时间,缺点就是当函数里面对参数进行了修改的时候返回后那个参数会改变,因为传引用是取地址的方式。