1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <vector>
#include <queue>

struct LabNode 
{
  int node;
  int lab; 
};

void add_edge(std::vector<LabNode> adj[], int u, int v, int l) 
{
  // Uwaga. Pozwala dodać wielokrotną krawędź (ale tu takich nie ma).
  LabNode vl = {v, l};
  adj[u].push_back(vl);
  LabNode ul = {u, l};
  adj[v].push_back(ul);
}

void del_edge(std::vector<LabNode> adj[], int u, int v) 
{
  std::vector<LabNode>::iterator it;
  for (it = adj[u].begin(); it != adj[u].end(); ++it) 
  {
    if (it->node == v) 
    {
      adj[u].erase(it);
      break;
    }
  }

  for (it = adj[v].begin(); it != adj[v].end(); ++it) 
  {
    if (it->node == u) 
    {
      adj[v].erase(it);
      break;
    }
  }
}

//void print_graph(std::vector<LabNode> adj[], int V)
//{
//  for (int v = 0; v < V; ++v) 
//  {
//    std::cout << "vertex " << v << " ";
//    for (int i = 0; i < adj[v].size(); i++)
//      std::cout << " -> " << adj[v][i].node << " (lab: " << adj[v][i].lab << ")";
//    std::cout << "\n";
//  }
//  std::cout << "\n";
//}

void change_color(std::vector<LabNode> adj[], 
                  int v, 
                  long long int z, 
                  int k, 
                  std::vector<int> &col) 
{
  // BFS
  std::vector<bool> visited(col.size(), false);
  std::vector<int> sum(col.size(), 0);
  std::queue<int> q;
  q.push(v);
  visited[v] = true;
  while (!q.empty()) 
  {
    int current = q.front();
    q.pop();
    if (sum[current] <= z) 
    {
      col[current] = k;
      // Etykiety są nieujemne. Można przestać, jak suma przekroczy z. 
      for (std::vector<LabNode>::iterator it = adj[current].begin();
           it != adj[current].end(); it++)
      {
        if (!visited[it->node]) 
        {
          visited[it->node] = true;
          sum[it->node] = sum[current] + it->lab;
          q.push(it->node);
        }
      }
    }
  }
}

void change_color(std::vector<LabNode> adj[], 
                  int v, 
                  int k,  
                  std::vector<int> &col) 
{  
  std::vector<bool> visited(col.size(), false);
  std::queue<int> q;
  q.push(v);
  visited[v] = true;
  while (!q.empty()) 
  {
    int current = q.front();
    q.pop();
    col[current] = k;
    for (std::vector<LabNode>::iterator it = adj[current].begin();
         it != adj[current].end(); it++)
    {
      if (!visited[it->node]) 
      {
        visited[it->node] = true;
        q.push(it->node);
      }
    }
  }
}

int main() 
{
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);  
  unsigned int n, m, q, m_curr;
  int a, b, d;
  std::cin >> n >> m >> q;
  std::vector<int> col(n, 0);
  std::vector<LabNode> adj[n];
  for (unsigned int i = 0; i < m; i++)
  {
    std::cin >> a >> b >> d;
    add_edge(adj, a - 1, b - 1, d);
    // print_graph(adj, n);
  }
  m_curr = m;
  for (unsigned int i = 0; i < q; i++)
  {
    short int op;
    std::cin >> op;
    switch (op)
    {
      case 1:
        std::cin >> a >> b >> d;
        add_edge(adj, a - 1, b - 1, d);
        m_curr++;
        // print_graph(adj, n);
        break;
      case 2:
        std::cin >> a >> b;
        del_edge(adj, a - 1, b - 1);
        m_curr--;
        // print_graph(adj, n);
        break;
      case 3:
        int v, k;
        long long int z;
        std::cin >> v >> z >> k;
        if (z < 1e15)
          change_color(adj, v - 1, z, k, col);
        else if (m_curr < n - 1) 
          // Wypełniamy wszystkie dostępne z wierzchołka
          change_color(adj, v - 1, k, col); 
        else // Drzewo.
        {
          for (unsigned j = 0; j < n; j++)
            col[j] = k;
        }
        break;
      default:  
        int u;
        std::cin >> u;    
        std::cout << col[u - 1] << "\n";
    }    
  }
  // system("pause");
}