예제 #1
0
// Read a graph from input in adjacency list form.
void read_adjacency_list( graph& g )
{
    for ( std::string line("x"); std::getline(std::cin, line); )
    {
        g.push_back( neighbors_t() );

        std::stringstream s(line);
        for ( vertex_id dst; s >> dst; )
            g.back()
             .push_back( dst );
    }
예제 #2
0
int main(void) {
	int n, i, j, d;
	for (cin >> n; n; --n) {
		g.push_back(vector<int> ());
		for (cin >> i >> d; d; --d) {
			cin >> j;
			g.back().push_back(j);
		}
	}
	cin >> i >> j;
	cout << i << " " << j << " " << parcours(i, j) << endl;
	return 0;
}
예제 #3
0
int kruskal(graph& g, int n, graph& mst) {
    // sort edges by weight
    sort(g.begin(), g.end(), [](const edge& a, const edge& b){ return a.w < b.w; });

    // split vertices into n subtrees
    vi root(n, 0);
    for (int i = 0; i < root.size(); i++) {
        root[i] = i;
    }

    int len = 0;
    for (auto& e : g) {
        int ru = root_of(e.u, root);
        int rv = root_of(e.v, root);
        if (ru != rv) {
            root[rv] = ru;
            len += e.w;
            mst.push_back(e);
        }
    }

    return len;
}