当前位置:网站首页>Draw a directed graph based on input

Draw a directed graph based on input

2022-07-01 06:28:00 Keep--Silent

explain

Draw one directly from the input mermaid

Source code

#include <bits/stdc++.h>
using namespace std;

int  main() {
    
    int m, j,v,w,x;
    map<int, map<int, int> >g;
    cin >> m;
    while (m--) {
    
        cin >> v >> w >> x;
        g[v][w] = x;
        //g[w][v]=x;
    }
    set<int>st;
    vector<int>t(3, 0);
    vector<vector<int>>vt;
    for (auto p1:g) {
    
        st.insert(p1.first);
        for (auto p2 : p1.second) {
    
            st.insert(p2.first);            
            vt.push_back(vector<int>{
    p1.first,p2.first,p2.second});
        }
    }
    printf("```mermaid\ngraph TD\n");
    for (auto x : st) {
    
        printf("%d([%d])\n",x,x);
    }
    for (auto v : vt) {
    
        printf("\t%d -->|%d| %d\n",v[0],v[2],v[1]);
    }
    printf("```\n");
	return 0;


}

Input and output

7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
	 ```mermaid
		graph TD
		1([1])
		2([2])
		3([3])
		4([4])
		5([5])
		        1 -->|2| 2
		        1 -->|1| 5
		        2 -->|1| 3
		        2 -->|1| 4
		        2 -->|2| 5
		        3 -->|1| 4
		        3 -->|1| 5
	 ```

effect

2
1
1
1
2
1
1
1
2
3
4
5
2
1
1
1
2
1
1
1
2
3
4
5
原网站

版权声明
本文为[Keep--Silent]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010625279254.html