imfkwk @ 2021-01-24 14:59:58
这题,大概可以用链表写。刚刚用vector和map过了两次,觉得vector的形式和链表非常相像。于是在题解里一个链表都没有的情况下,画蛇添足地写了链表,贡献了几次提交次数和几个红色的20分。
我的链表究竟哪里有问题啊!
#include <bits/stdc++.h>
using namespace std;
int num;
int head[100001];
int from[10000001];
int item[10000001];
int no[10000001];
int n, q;
int main(void) {
scanf("%d%d", &n, &q);
while (q--) {
int k, x, y, z;
scanf("%d", &k);
if (k == 1) {
scanf("%d%d%d", &x, &y, &z);
bool f = 1;
int p = head[x];
while (p) {
if (no[p] == y) {
f = 0;
item[p] == z;
break;
}
p = from[p];
}
if (f) {
++ num;
from[num] = head[x];
item[num] = z;
no[num] = y;
head[x] = num;
}
} else if (k == 2) {
scanf("%d%d", &x, &y);
int p = head[x];
while (p) {
if (no[p] == y) {
printf("%d\n", item[p]);
break;
}
p = from[p];
}
}
}
return 0;
}
思路和vector是一样的。先搜索这个柜子里存没存东西,存了就替换,没存就加一个。(看了题解发现这种行为多此一举,因为可以从后往前搜索,这样搜索到的就是最终状态)
by 123456zmy @ 2021-01-24 15:33:55
(打错了,是
by imfkwk @ 2021-01-24 15:43:06
@123456zmy 那路或多
by imfkwk @ 2021-01-24 16:40:27
然后我写了个没啥区别的
#include <bits/stdc++.h>
using namespace std;
int num;
int head[100001];
int from[10000001];
int item[10000001];
int no[10000001];
int n, q;
int main(void) {
cin >> n >> q;
while (q--) {
int k, x, y, z;
cin >> k;
if (k == 1) {
cin >> x >> y >> z;
++num;
from[num] = head[x];
item[num] = z;
no[num] = y;
head[x] = num;
} else if (k == 2) {
cin >> x >> y;
int p = head[x];
while (p) {
if (no[p] == y) {
cout << item[p] << endl;
}
p = from[p];
}
}
}
return 0;
}
by 514InParadox @ 2021-01-24 16:59:11
此人的名言
太丢人了
第27行:
item[p] == z;
by imfkwk @ 2021-01-24 17:04:51
@514InParadise 此人还有一句名言:
“我是**”