比赛 |
寒假归来,刮刮油 |
评测结果 |
AAAAAAAAAA |
题目名称 |
分糖果 |
最终得分 |
100 |
用户昵称 |
Menci |
运行时间 |
3.074 s |
代码语言 |
C++ |
内存使用 |
1.07 MiB |
提交时间 |
2016-02-25 20:56:31 |
显示代码纯文本
#include <cstdio>
#include <climits>
#include <algorithm>
#include <queue>
const int MAXN = 100000;
const int MAXM = 1000010;
struct Node;
struct Edge;
struct Node {
Edge *firstEdge;
int dist;
} nodes[MAXN];
struct Edge {
Node *from, *to;
Edge *next;
Edge(Node *from, Node *to) : from(from), to(to), next(from->firstEdge) {}
};
int n, m, start, t;
inline void addEdge(int u, int v) {
nodes[u].firstEdge = new Edge(&nodes[u], &nodes[v]);
nodes[v].firstEdge = new Edge(&nodes[v], &nodes[u]);
}
inline int bfs(int start) {
for (int i = 0; i < n; i++) nodes[i].dist = INT_MAX;
nodes[start].dist = 1;
std::queue<Node *> q;
q.push(&nodes[start]);
while (!q.empty()) {
Node *v = q.front();
q.pop();
for (Edge *e = v->firstEdge; e; e = e->next) {
if (e->to->dist == INT_MAX) {
e->to->dist = v->dist + 1;
q.push(e->to);
}
}
}
int max = 0;
for (int i = 0; i < n; i++) max = std::max(max, nodes[i].dist);
return max;
}
int main() {
freopen("dividecandy.in", "r", stdin);
freopen("dividecandy.out", "w", stdout);
scanf("%d %d %d %d", &n, &m, &start, &t);
start--;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v), u--, v--;
addEdge(u, v);
}
printf("%d\n", bfs(start) + t);
fclose(stdin);
fclose(stdout);
return 0;
}