#include<stdlib.h> #include<string.h> #include<stdio.h> typedef struct Node{ int data;//存储数据 int pre;//存储前一个节点的地址 int next;//存储下一个节点的地址 }node; int main() { node str[100005]; int i , n , ID , temp; scanf("%d%d",&ID , &n); for(i = 0 ; i < n ; i ++) { int ID1 , num , next; scanf("%d%d%d",&ID1 , &num , &next); str[ID1].data = num; str[ID1].next = next;//保存下一个节点的地址 if(next != -1) str[next].pre = ID1;//储存前一个节点的地址 if(next == -1) { temp = ID1;//记录最后一个节点的地址 } } for(;;) { printf("%05d %d ",temp , str[temp].data); if(temp == ID) { printf("-1\n"); break; } else printf("%05d\n",ID); temp = str[temp].pre; printf("%05d %d ",ID , str[ID].data); if(ID == temp) { printf("-1\n"); break; } else printf("%05d\n",temp); ID = str[ID].next; } return 0; }
// 图论 得分 25分(25分) // 邻接表保存,注意颜色个数需要等于k #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <string> #include <set> #include <algorithm> using namespace std; int v, e, k; //顶点数、边数、颜色数 vector<int> vec[505]; int book[505]; //记录每个顶点被标记颜色 set<int> s; //记录颜色种数 bool Judge() { if (s.size() != k) return false; //颜色个数不等于k时,返回false for (int i = 1; i <= v; i++) { for (int j = 0; j < vec[i].size(); j++) { if (book[i] == book[vec[i][j]]) //表示有相邻结点颜色相同 { return false; } } } return true; } int main() { cin >> v >> e >> k; for (int i = 0; i < e; i++) { int u, v; cin >> u >> v; vec[u].push_back(v); vec[v].push_back(u); } int t; //方案数 cin >> t; while (t--) { s.clear(); //注意清空 memset(book, 0, sizeof(book)); for (int i = 1; i <= v; i++) { int a; cin >> a; book[i] = a; s.insert(a); } bool flag = Judge(); if (flag) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
标签:temp,int,next,str,include,ID,8.12 From: https://www.cnblogs.com/xuxingkai/p/17625349.html